简体   繁体   English

用Java读/写文件

[英]read/write files in Java

How can I read a file easily in Java if I have following file format: 如果我有以下文件格式,如何用Java轻松读取文件:

a|dip
a|dop
c|nap
a|dip
b|dop
b|sip
a|tang
c|dig
c|nap

I want to get all words that belongs to "a", "b", and "c". 我想得到属于“a”,“b”和“c”的所有单词。 What data structure I can use to read and store this information? 我可以用什么数据结构来读取和存储这些信息?

You can also suggest some good file formats (two column) that is easy to read/write in Java. 您还可以建议一些易于在Java中读/写的良好文件格式(两列)。

I know some of you may be thinking that what is the real problem that I want to solve, I have some complex employee related data. 我知道有些人可能在想我想要解决的真正问题是什么,我有一些复杂的员工相关数据。 Current (poor) system generate some files and I am trying to process them to add them in database. 当前(差)系统生成一些文件,我正在尝试处理它们以将它们添加到数据库中。 The current files' format is bit complex (private), I cannot copy past here. 当前文件的格式有点复杂(私有),我无法复制过去。

If you can use Google Guava ( http://code.google.com/p/guava-libraries/ ) then you'll get a few handy classes (you can use some or all of these): 如果您可以使用Google Guava( http://code.google.com/p/guava-libraries/ ),那么您将获得一些方便的课程(您可以使用其中的部分或全部):

  1. com.google.common.io.Files
  2. com.google.common.io.LineProcessor<T>
  3. com.google.common.base.Charsets
  4. com.google.common.collect.Multimap<K,V>
  5. com.google.common.collect.ArrayListMultimap<K,V>

For example you could write: 例如,您可以写:

LineProcessor<Multimap<String, String>> processor = 
    new LineProcessor<Multimap<String, String>>() {
      Multimap<String, String> processed = ArrayListMultimap.create();

      public boolean processLine(String line) {
        String parts[] = line.split("\\|", 2); // 2 keeps any | in the rest of the line
        processed.put(parts[0], parts[1]);
        return true; // keep going
      }

      public Multimap<String, String> getResult() {
        return processed;
      }
    };

Multimap<String, String> result = Files.readLines(
    new File("filename.txt"), Charsets.UTF_8, processor);

You can use Scanner to read the text file one line at a time and then you can use String.split("\\\\|") to separate the parts on that line. 您可以使用Scanner一次读取一行文本文件,然后可以使用String.split("\\\\|")来分隔该行上的部分。 For storing the information, a Map<String,List<String>> might work. 为了存储信息, Map<String,List<String>>可能有效。

I'd use this data structure: 我使用这个数据结构:

Map<String, List<String>> map = new HashMap<String, List<String>>();

And parse the file like this: 并解析文件,如下所示:

File file = new File("words.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
    String next = scanner.next();
    String[] parts = next.split("\\|");
    String group = parts[0];
    String word = parts[1];

    List<String> list = map.get(group);
    if (list == null) {
        list = new ArrayList<String>();
        map.put(group, list);
    }
    list.add(word);
}

So you could get the list of words for "a" like so: 所以你可以像这样获得“a”的单词列表:

for (String word : map.get("a")) {
    System.out.println(word);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM