public String read(String filename){
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
String temp = null;
StringBuilder sb = new StringBuilder();
while( (temp = in.readLine()) != null ){
sb.append(temp+"\n");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
读文件注意:
- BufferedReader才有readLine(),Reader只有read()
- while( (s=in.readLine())!=null )
- in.close()不能在finally里面,因为close会抛IOException
- readLine()的结果是不带有\n的,必要时要加上\n
- 写文件,格式化时用PrintWriter