I have tried to implement a bufferedreader in order to convert a .txt file, specifically the Iliad to a string. I have tested small files and they have worked but the larger do not. When I attempt to print fileString after the while loop it finished, no output is shown. Here's my code.
我试图实现一个bufferedreader,以便将.txt文件,特别是Iliad转换为字符串。我已经测试了小文件,但它们已经有效,但较大的文件没有。当我尝试在while循环完成后打印fileString时,没有显示输出。这是我的代码。
String fileString = "";
String line = "";
char readChar;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(inputFile));
while((line=br.readLine())!=null)
{
fileString = fileString + line;System.out.println(fileString);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}System.out.println(fileString);
2 个解决方案
#1
2
Recall that Strings are immutable in java. This means that the way you are constructing the String from by +
is extremely inefficient and resource costly.
回想一下,字符串在java中是不可变的。这意味着您从by +构造String的方式效率极低且资源成本高。
You can use either StringBuilder or StringBuffer. In my example I use StringBuilder since it does not seem that you need to worry about synchronization.
您可以使用StringBuilder或StringBuffer。在我的例子中,我使用StringBuilder,因为它似乎不需要担心同步。
StringBuilder fileString = new StringBuilder();
String line = "";
char readChar;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(inputFile));
while((line=br.readLine())!=null)
{
fileString.append(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileString.toString());
Try this. Although, I am not sure whether println function will be able to print the whole string.
试试这个。虽然,我不确定println函数是否能够打印整个字符串。
#2
0
You are doing string concatenation the slow way. The very slow way. Use a StringBuffer
or StringBuilder
.
你正在慢慢地进行字符串连接。非常缓慢的方式。使用StringBuffer或StringBuilder。
It has exactly nothing to do with BufferedReader
whatsoever, as a simple test will show: remove the code in the body of the loop.
它与BufferedReader完全没有任何关系,因为一个简单的测试将显示:删除循环体中的代码。
#1
2
Recall that Strings are immutable in java. This means that the way you are constructing the String from by +
is extremely inefficient and resource costly.
回想一下,字符串在java中是不可变的。这意味着您从by +构造String的方式效率极低且资源成本高。
You can use either StringBuilder or StringBuffer. In my example I use StringBuilder since it does not seem that you need to worry about synchronization.
您可以使用StringBuilder或StringBuffer。在我的例子中,我使用StringBuilder,因为它似乎不需要担心同步。
StringBuilder fileString = new StringBuilder();
String line = "";
char readChar;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(inputFile));
while((line=br.readLine())!=null)
{
fileString.append(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileString.toString());
Try this. Although, I am not sure whether println function will be able to print the whole string.
试试这个。虽然,我不确定println函数是否能够打印整个字符串。
#2
0
You are doing string concatenation the slow way. The very slow way. Use a StringBuffer
or StringBuilder
.
你正在慢慢地进行字符串连接。非常缓慢的方式。使用StringBuffer或StringBuilder。
It has exactly nothing to do with BufferedReader
whatsoever, as a simple test will show: remove the code in the body of the loop.
它与BufferedReader完全没有任何关系,因为一个简单的测试将显示:删除循环体中的代码。