java怎样读入一个文本文件保存到一个字符串
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileTest1 {
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new FileReader("test.txt"));
StringBuilder result=new StringBuilder();
String theLine=null;
while(( theLine=reader.readLine())!=null){
result.append(theLine+"\n");
}
System.out.println(result.toString());
}
}
好用的方法如下:
File f=new File("");
BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(f))); Stirng s=null;
String content=null;
while((s=read.readLine())!=null){
content+=s+"\r\n";
}