【问题标题】:Using StringBuilder and BufferedReader, how can I add commas after each word in a text file and produce a string?使用 StringBuilder 和 BufferedReader,如何在文本文件中的每个单词后添加逗号并生成字符串?
【发布时间】:2015-04-21 05:34:52
【问题描述】:

我在一个文本文件中有一个基因名称列表,它们都在一列中。我想生成一个由逗号分隔的所有基因名称的字符串。我使用另一个线程来帮助我做到这一点,但我一直在每个名字之间有两个逗号。看起来它在每个名称之前和之后添加了一个逗号。任何指导将不胜感激!下面是代码sn-p!

      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());

               }

【问题讨论】:

  • 您已经注释掉了line.append(", ");,所以我认为这段代码不会在任何地方添加任何逗号。不要只是在此处粘贴这样的代码,它显然不会像您声称的那样做。
  • 你的代码有什么问题?
  • 对不起Kayaman,这是我第一个关于堆栈溢出的问题。我在尝试修复它时将其注释掉。

标签: java delimiter stringbuilder comma


【解决方案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, ',');

【讨论】:

  • 或 Java 8 中的 StringJoiner
【解决方案2】:

摆脱所有这些:

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


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

并将其替换为:

 line.append(text + ",");

另外,最好将您的 text 变量更改为:

String text = "";

我不喜欢创建字符串对象null。这不是一个好习惯。

【讨论】:

  • 非常感谢!我试图单独附加逗号。这解决了它!
  • 太棒了。很高兴我能帮上忙。您能否将其标记为已接受的答案(只需点击我的答案旁边的复选标记)?
【解决方案3】:

您可以使用 Java 8 java.util.StringJoiner:

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

【讨论】:

    【解决方案4】:

    有几个简单的方法...

    如果您将整个文件作为一个大字符串(例如使用 apache commons-io),您可以这样做:

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

    或者,如果您将文件作为List&lt;String&gt;:

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

    【讨论】:

      【解决方案5】:

      也许这会对你有所帮助。

      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());
      

      【讨论】:

        猜你喜欢
        • 2017-07-25
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 2023-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多