简体   繁体   中英

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()); 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 (String conf: items.keySet())
        {
            System.out.println(conf + " | " + items.get(conf));
        }
      } // end of while

Do that instead: move the for outside the while loop.

      } // 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:

        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> . An entry being an association between a key and its value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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