FileReader读取txt文件

第一种方法

private static void ReadFile(String filePath) {
try {
StringBuilder stringBuilder = new StringBuilder(50);
FileReader fileReader = new FileReader(filePath);
int i;
while ( (i = fileReader.read()) != -1) {
stringBuilder.append((char)i);
}
System.out.println(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

第二种方法

private static void ReadFile(String filePath) {
try {
StringBuilder stringBuilder = new StringBuilder(50);
FileReader fileReader = new FileReader(filePath);
char[] bytes = new char[1024];
while (fileReader.read(bytes) != -1) {
stringBuilder.append(bytes);
}
System.out.println(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

上一篇:IO之字符流


下一篇:复制文件的异常处理