Using StringBuilder and BufferedReader, how to add commas after each word to a text file and create a line?

I have a list of gene names in a text file that is in one column. I would like to create a String of all gene names, separated by commas. I used a different thread to help me get this far, but I keep getting two commas between the names. It seems like this is adding a comma before and after each name. Any guidance would be appreciated! Below is a snippet of code.

      File file = new File ("/Users/Maddy/Desktop/genelist.txt");
                  StringBuilder line = new StringBuilder();
                  BufferedReader reader = null;

            try {
                reader = new BufferedReader (new FileReader(file));
                String text = null;

                while ((text = reader.readLine()) !=null) {
                     line.append(text);
                    line.append(System.getProperty ("line.separator"));


             //line.append(text);
            //line.append(", ");
                }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally 
               {
                try {
                    if (reader !=null){
                        reader.close();
                }
                }
                catch (IOException e) 
                {
                  e.printStackTrace();
                }
                }
            System.out.println(line.toString());
            String _input= new String(line.toString());

               }
+4
source share
5 answers

Get rid of all this:

 line.append(text);
 line.append(System.getProperty ("line.separator"));


 //line.append(text);
 //line.append(", ");

And replace it with this:

 line.append(text + ",");

Also, it would be better to change your variable textto:

String text = "";

String null. .

+1

, Apache commons

List<String> slist = new ArrayList<String> ();
StringBuilder rString = new StringBuilder();
while ((text = reader.readLine()) !=null) {
           sList.add(text);     
}
String fullString = StringUtils.join(slist, ',');
+2

Java 8 java.util.StringJoiner:

    StringJoiner j = new StringJoiner(",");
    for (String line; (line = reader.readLine()) != null;) {
        j.add(line);
    }
    String fullString = j.toString();
0
source

There are some easy ways ...

If you get the whole file as one big line (say using apache commons-io), you can do this:

String csv = rawString.replaceAll("(?ms)$.*?^", ", ");

Or, if you have a file like List<String>:

List<String> lines;
String csv = lines.stream().collect(Collectors.joining(", "));
0
source

Perhaps this will help you.

StringBuilder builder = new StringBuilder();
    int count = 0;
    List<String> list = Files.readAllLines(Paths.get("C:","Users/Maddy/Desktop/genelist.txt"));
    for (String line : list) {
        ++count;
        builder.append(line.trim());
        if (count != list.size()) {
            builder.append(",");
        }
    }
    System.out.println(builder.toString());
0
source

All Articles