- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在一个文本文件中有一个基因名称列表,它们都在一列中。我想生成一个由逗号分隔的所有基因名称的字符串。我使用了另一个线程来帮助我走到这一步,但我一直在每个名字之间得到两个逗号。看起来它在每个名称前后都添加了一个逗号。任何指导将不胜感激!代码片段如下!
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());
}
最佳答案
这是使用 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 - 使用 StringBuilder 和 BufferedReader,如何在文本文件中的每个单词后添加逗号并生成字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763760/
公共(public)类TestMyStringBuilderII { public static void main(String[] args) { StringBuilder sb = n
我们都知道字符串是不可变的而StringBuilder是可变的。正确的。那么为什么它的方法会返回一个 StringBuilder 对象。它们不应该都是 void 方法吗? 为什么会这样 public
当我读到 equals() 方法用于比较 java 中的字符串是否相等但是当我运行这段代码时我得到输出 false 。为什么? public class TestStringBuilder { pu
我收到一份模型列表。模型的数量可能很大。这个模型有一堆属性,其中任何一个都可能是null。 我需要根据模型的属性为每个模型构建一个字符串。如果 property == null,那么我会在结果字符串中
我写了一些代码,其中有很多字符串创建。为了尽量避免一遍又一遍地创建昂贵的字符串,我使用了 java StringBuilder 类。 我的代码有点像这样: public String createSt
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: StringBuilder vs String concatenation in toString() in Ja
类 StringBuilder 定义了四个构造函数,它们都不接受 StringBuilder,但以下编译: StringBuilder sb = new StringBuilder(new Strin
我正在努力在我的代码中使用 StringBuilder,而不是 String使代码在所有解析和连接过程中具有时间效率。 但是当我查看它的源代码时,substring() 方法AbstractStrin
我想知道 StringBuilder,我有一个问题希望社区能够解释。 让我们忘掉代码的可读性,哪些是更快,为什么? StringBuilder.Append: StringBuilder sb = n
这个问题在这里已经有了答案: Single exclamation mark in Kotlin (6 个回答) Example of when should we use run, let, app
我已经附加了一些带有一些字符的 strBuild 并将该 strBuild 放入 stringBuildArr 中。然后在strBuild上调用java.lang.StringBuilder.dele
在 C# 中哪个内存效率更高:选项 #1 还是选项 #2? public void TestStringBuilder() { //potentially a collection with
当使用 StringBuilder.ToString() 时,我遇到了 OutOfMemory 异常。因为我的字符串需要很大的空间。这就是为什么我需要一种方法(也许通过流式传输)来让它工作。 这是我的
这个问题已经有答案了: What's the difference between instance method reference types in Java 8? (3 个回答) 已关闭 7 年
这个问题在这里已经有了答案: What's the difference between instance method reference types in Java 8? (3 个答案) 关闭
最近的 question came up关于使用 String.Format()。我的部分回答包括使用 StringBuilder.AppendLine(string.Format(...)) 的建议
在 SO 上看到一个关于连接字符串的问题后,我做了一些测试,了解到在 foreach 中连接字符串比使用 for 循环和使用数组中的索引慢。由于对数组进行绑定(bind)检查,for 循环不应该变慢吗
我的问题是如果我有时在同一个字符串上使用多线程 字符串不会被替换。(我在记事本上写了这个,所以语法可能是 错误) 使用 System.Thread ...当然还有其他 class .... {
如果我创建一个不可变类。所有字段都必须是最终的。如果我像这样使用 stringbuilder final StringBuilder s = new StringBuilder("你好"); ,那么
是否可以配置 Java StringBuilder类在每次调用之间追加一个新行以追加? 最佳答案 我不认为 StringBuilder内置了该功能。既然你不能只是扩展它,你可以尝试使用装饰器模式...
我是一名优秀的程序员,十分优秀!