简体   繁体   English

从Java中的文本文件读取行并拆分内容

[英]Reading lines from text file in Java and splitting the content

I have an input files as follows: 我有一个输入文件,如下所示:

conf/iastedCSN/KangHPLNL06 Quoc V. Phung    
conf/iastedCSN/KangHPLNL06 Kungmeng Lo  
conf/iastedCSN/KangHPLNL06 Hoang Nam Nguyen 
conf/iastedCSN/KangHPLNL06 M. M. Lee    
series/sci/ZighedAB13 Djamel Abdelkader Zighed  
series/sci/ZighedAB13 Rafik Abdesselam  
series/sci/ZighedAB13 Ahmed Bounekkar   
series/sci/LermanG13 Isra챘l-C챕sar Lerman

And I want to the output with delimiter as follows: 我想用定界符输出如下:

conf/iastedCSN/KangHPLNL06 | QuocV.Phung    
conf/iastedCSN/KangHPLNL06 | KungmengLo 
conf/iastedCSN/KangHPLNL06 | HoangNamNguyen 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | DjamelAbdelkaderZighed  
series/sci/ZighedAB13 | RafikAbdesselam 
series/sci/ZighedAB13 | AhmedBounekkar  
series/sci/LermanG13 | Isra챘l-C챕sarLerman

However for now, I got lots of lines duplicated in the result and I could not seem to find out why. 但是,到目前为止,结果中有很多行重复,而且我似乎找不到原因。 The output I got as follows: 我得到的输出如下:

conf/iastedCSN/KangHPLNL06 | QuocV.Phung    
conf/iastedCSN/KangHPLNL06 | KungmengLo 
conf/iastedCSN/KangHPLNL06 | HoangNamNguyen 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | DjamelAbdelkaderZighed  
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | RafikAbdesselam 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | AhmedBounekkar  
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/LermanG13 | Isra챘l-C챕sarLerman   
series/sci/ZighedAB13 | AhmedBounekkar  

Below is the source code: 下面是源代码:

package authorgraph;
import java.io.*;
import java.util.*;

public class graph {
    private static BufferedReader br;

    public static void main(String[] args)
    {
        try{
        br = new BufferedReader (new FileReader ("inproceedings-author-test1.txt"));
        Map<String, String>  items = new TreeMap<String, String>();
        String line;

        while (null != (line = br.readLine()))
        {
            String[] line_parts = line.split(" ");
            if (line_parts.length > 1)
            {
                StringBuilder name = new StringBuilder(line_parts[1]);
                for (int i = 2; i < line_parts.length; i++)
                {
                    name.append(line_parts[i]);
                }
                items.put(new String(line_parts[0]), name.toString());
            }
            for (String conf: items.keySet())
            {
                System.out.println(conf + " | " + items.get(conf));
            }
        }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
};

Any help would be really appreciated 任何帮助将非常感激

The new String in items.put(new String(line_parts[0]), name.toString()); new Stringitems.put(new String(line_parts[0]), name.toString()); is useless. 是没用的。 String being immutable, if you already have a String, there is no need to create a new one. 字符串是不可变的,如果您已经有一个字符串,则无需创建一个新的字符串。

And you got duplicate because the for is not placed at the right place: you are executing it each time you read a line. 而且您得到了重复,因为for位置不正确:每次读取一行时都在执行它。

        for (String conf: items.keySet())
        {
            System.out.println(conf + " | " + items.get(conf));
        }
      } // end of while

Do that instead: move the for outside the while loop. 而是这样做:将for移到while循环之外。

      } // end of while
        for (String conf: items.keySet())
        {
            System.out.println(conf + " | " + items.get(conf));
        }

And instead of doing a loop on keySet() , do that: 并且不要在keySet()上循环,而是这样做:

        for (Map.Entry<String, String> entry : items.entrySet()) {
            System.out.println(entry.getKey() + " | " + entry.getValue());
        }

Map<K,V>.entrySet() will return a Map.Entry<K,V> . Map<K,V>.entrySet()将返回Map.Entry<K,V> An entry being an association between a key and its value. 条目是键及其值之间的关联。

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

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