import java.io.FileReader;
import java.io.IOException;
public class FileReaderCls {
public static void main(String[] args) {
readFile01();
readFile02();
}
//单个字符读取
public static void readFile01(){
FileReader fileReader = null;
String filePath = "/Users/chen/Files/test.txt";
int data = ' ';
try {
fileReader = new FileReader(filePath);
while((data = fileReader.read()) != -1){
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
//多个字符读取 字符数组
public static void readFile02(){
FileReader fileReader = null;
String filePath = "/Users/chen/Files/test.txt";
int readLen = 0;
char[] buf = new char[8];
try {
fileReader = new FileReader(filePath);
while((readLen = fileReader.read(buf)) != -1){
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}