大家看下这个代码:
import java.io.*;public class FileToString {
public static String readFile(String fileName) {
String output = "";
File file = new File(fileName);
if(file.exists()){
if(file.isFile()){
try{
BufferedReader input = new BufferedReader (new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text; //问题出在这2行,如果我这么写的话,就会报那个错误。
//String text = input.readLine();
while((text = input.readLine()) != null) //while(text != null)
buffer.append(text +"\n");
output = buffer.toString();
}
catch(IOException ioException){
System.err.println("File Error!"); }
}
else if(file.isDirectory()){
String[] dir = file.list();
output += "Directory contents:\n";
for(int i=0; i<dir.length; i++){
output += dir[i] +"\n";
}
}
}
else{
System.err.println("Does not exist!");
}
return output;
}
public static void main (String args[]){
String str = readFile("C:/1.txt");
System.out.print(str);
}
}奥运加油!北京加油!中国加油!
我当时觉得按注释那样写,也没什么错误啊,结果运行就出错了 报java.lang.OutOfMemoryError的错误
import java.io.*;public class FileToString {
public static String readFile(String fileName) {
String output = "";
File file = new File(fileName);
if(file.exists()){
if(file.isFile()){
try{
BufferedReader input = new BufferedReader (new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text; //问题出在这2行,如果我这么写的话,就会报那个错误。
//String text = input.readLine();
while((text = input.readLine()) != null) //while(text != null)
buffer.append(text +"\n");
output = buffer.toString();
}
catch(IOException ioException){
System.err.println("File Error!"); }
}
else if(file.isDirectory()){
String[] dir = file.list();
output += "Directory contents:\n";
for(int i=0; i<dir.length; i++){
output += dir[i] +"\n";
}
}
}
else{
System.err.println("Does not exist!");
}
return output;
}
public static void main (String args[]){
String str = readFile("C:/1.txt");
System.out.print(str);
}
}奥运加油!北京加油!中国加油!
我当时觉得按注释那样写,也没什么错误啊,结果运行就出错了 报java.lang.OutOfMemoryError的错误
最多的不同就是读到最后一行 大不了text为空嘛...
也就是text说不可能变为null
while(text != null)
这样写只是把你的文件第一行不停的读入而已. 所以会报 out错误.
while(text != null)
这样写只是把你的文件第一行不停的读入而已. 所以会报 out错误.
如果这个text不为null
那么//while(text != null)就相当于while(true)
所死循环 while((text = input.readLine()) != null)
是每循环一次读取一行新的数据赋给text
所以每次text都是新的值
当读到最后的时候text==null
结束循环
String text = input.readLine();
while(text!= null) {
buffer.append(text +"\n");
text = input.readLine();
}
---------------------
2.或者合成一句
while ((text=input.readLine()) != null)
--------------------
如果按照注释那样写,readLine只执行了一次,所以text永远也不为null,所以死--循-环-
while((text = input.readLine()) != null)
这样写的话只要input流里一直有一行数据
所以就不为null,所以不停的循环
不能这么改,这么改就错了,这样相当于第一行没有取到!
如果实在要改的话:while((text!= null) {
buffer.append(text +"\n");
text = input.readLine();
}
给一个测试:
//test
String text = input.readLine();
int counter = 1;
long startTime = System.nanoTime();
while(text != null){
buffer.append(text + "\n");
long endTime = System.nanoTime();
System.out.println(counter + ": " + (endTime - startTime) + "ns");
counter++;
}运行结果:132917: 4396824736ns
132918: 4396842336ns
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
StringBuffer buffer = new StringBuffer();
顾名思义,用缓存的方式来处理数据,如果数据较大肯定会OutOfMemoryError.
不要缓存,读一句写一句就不会OutOfMemoryError.