读取文件并用Java分割行. [英] Read a file and split lines in Java.

查看:217
本文介绍了读取文件并用Java分割行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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.

预先感谢...

推荐答案

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]);
    }
}

使用此代码,您在列表"frenchlist"中具有法语单词,在列表"englishlist"中具有英语单词

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

这篇关于读取文件并用Java分割行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆