Java 文件的读取与写入
## 读取文件
### 以字节为单位读取文件(FileInputStream)
常用于读二进制文件,如图片、声音、影像等文件。
```
File file = new File("filePath");
try (InputStream in = new FileInputStream(file);) {
// 一次读4个字节
byte[] bytes = new byte[4];
// 读取到的字节数量
int readCount = 0;
// 读入4个字节到字节数组中
while ((readCount = in.read(bytes)) != -1) {
System.out.write(bytes, 0, readCount);
}
} catch (Exception e) {
e.printStackTrace();
}
```
### 以字符为单位读取文件(InputStreamReader)
常用于读文本,数字等类型的文件。
```
File file = new File("filePath");
try (Reader reader = new InputStreamReader(new FileInputStream(file));) {
// 一次读30个字符
char[] chars = new char[30];
//