public class FileMessage2 {
public static void main(String[] args) throws Exception {
File file = new File("D:"+File.separator+"test.txt");
if(!file.exists()){
throw new Exception("抱歉,您请求的文件路径不存在!");
}
// InputStream is = new FileInputStream(file);
// byte[] byt = new byte[(int)file.length()];
// is.read(byt);
// is.close();
// String result = new String(byt);
// result.replace("rn", "");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String str;
while((str=br.readLine())!=null){
sb.append(str);
}
br.close();
fr.close();
System.out.println("从文件中读到的结果:"+sb);
}
}
采用了一种按行读取的方式解决了,但是我还是不知道一开始的写法里replace()方法为何无效,悲:-(