在 Java 中读取文件并拆分行。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20841980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:44:11  来源:igfitidea点击:

Read a file and split lines in Java.

javatextfile-iosplit

提问by user3144878

Hello, I want to read a file, file.txt that contains word pairs like this...

你好,我想读取一个文件,file.txt,其中包含这样的词对......

mot;word 
oui;yes
utiliser;use
comment;how

After reading this file.txt , I want to split this text and put the French words in an ArrayList and the English words in an another ArrayList.

阅读此 file.txt 后,我​​想拆分此文本并将法语单词放在一个 ArrayList 中,将英语单词放在另一个 ArrayList 中。

Thanks in advance...

提前致谢...

回答by user2336315

mot;word 
oui;yes
utiliser;use
comment;how

It appears that the structure of each line is

看起来每一行的结构是

frenchWord;englishWord

So you can read each line of your file using a Scanner(using the constructor Scanner(File source)) and the nextLine()method, and spliteach line by ";".

因此,您可以使用Scanner(使用构造函数Scanner(File source))和nextLine()方法读取文件的split每一行,并且每行都使用";".

The first element in the array will be the french word and the second one the english word.

数组中的第一个元素是法语单词,第二个元素是英语单词。

Add those element in two separate List(ArrayListper example), one containing all the french words, and the other one the english words.

将这些元素添加到两个单独的ListArrayList每个示例)中,一个包含所有法语单词,另一个包含英语单词。

回答by MariuszS

This looks like CSV file. Consider using CSV reader library.

这看起来像 CSV 文件。考虑使用 CSV 阅读器库。

Use String#splitfunction from JDK and read file line by line with Scanner:

使用JDK 中的String#split函数并使用Scanner逐行读取文件:

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  // process the line using String#split function
}

In whileloop add splited data to ArrayList.

while循环中将拆分的数据添加到ArrayList.

All information are already on stackoverflow.

所有信息都已经在 stackoverflow 上。

回答by bgamlath

First, you have to create two array lists..

首先,您必须创建两个数组列表。

    ArrayList<String> english = new ArrayList<>();
    ArrayList<String> french = new ArrayList<>();

Then, open the file, read line by line, split it bye ";" and add the words to ArrayLists...

然后,打开文件,逐行读取,将其拆分为“;” 并将单词添加到 ArrayLists...

    try(BufferedReader in = new BufferedReader(new FileReader("file.txt"))){
      String line;
      while((line = in.readLine())!=null){
          String[] pair = line.split(";");
          french.add(pair[0]);
          english.add(pair[1]);
      }
    }

回答by beny1700

public static void main(String[] args) {
    List<String> list = new ArrayList<String>(); 
    List<String> frenchList = new ArrayList<String>();
    List<String> englishList = new ArrayList<String>();
    File file = new File("C:/dico.txt");
    if(file.exists()){
        try { 
            list = Files.readAllLines(file.toPath(),Charset.defaultCharset());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
      if(list.isEmpty())
          return;
    }
    for(String line : list){
        String [] res = line.split(";");
        frenchList.add(res[0]);
        englishList.add(res[1]);
    }
}

With this code you have de french word in the list "frenchlist" and the english words in the list "englishlist"

使用此代码,您可以在“frenchlist”列表中找到法语单词,在“englishlist”列表中使用英语单词