读取文件并用Java分割行.

use*_*878 1 java file-io text split

您好,我想读一个文件,包含这样的单词对的file.txt ...

mot;word 
oui;yes
utiliser;use
comment;how
Run Code Online (Sandbox Code Playgroud)

读完这个file.txt之后,我想拆分这个文本,把法语单词放在一个ArrayList中,把英文单词放在另一个ArrayList中.

提前致谢...

ben*_*700 8

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,您可以在"frenchlist"列表中使用de french word,并在列表"englishlist"中使用英语单词