常用的文件操作
![image-20230829150940349](https://img-blog.csdnimg.cn/img_convert/57345369c67c1f61fc32b2b77ab56327.png)
![image-20230829151010566](https://img-blog.csdnimg.cn/img_convert/21de945ce0cff56b7a6de77bea292db5.png)
public class FileCreate {
public static void main(String[] args) {
}
@Test
public void create01() {
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create02() {
File parentFile = new File("e:\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create03() {
String parentPath = "e:\\";
String fileName = "news4.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}
![image-20230829151148077](https://img-blog.csdnimg.cn/img_convert/b2d38a988e7ac1f636bcb0b8c9d13da1.png)
![image-20230829151153805](https://img-blog.csdnimg.cn/img_convert/fc279e8d49718ef115baeccaf6e582d3.png)
![image-20230829151225802](https://img-blog.csdnimg.cn/img_convert/f0c4d679df76e1636ba1da43eaef765f.png)
流的分类
![image-20230829151319534](https://img-blog.csdnimg.cn/img_convert/b63860aad51bc6842f313275635f9046.png)
![image-20230829151333330](https://img-blog.csdnimg.cn/img_convert/0787391d237ebd5347117415504ec16b.png)
FileInputStream
@Test
public void readFile01() {
String filePath = "e:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02() {
String filePath = "e:\\hello.txt";
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileOutputStream
![image-20230829152619042](https://img-blog.csdnimg.cn/img_convert/38fe90193e4ba5066489cc064c87600e.png)
public void writeFile() {
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath, true);
String str = "hsp,world!";
fileOutputStream.write(str.getBytes(), 0, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReader 和 FileWriter
![image-20230829152957157](https://img-blog.csdnimg.cn/img_convert/975b13e4ee26e5fc289ab8470b6710ea.png)
![image-20230829153515354](https://img-blog.csdnimg.cn/img_convert/6cd8c1e9b2c6ef0e90d0156d6be6fb07.png)
![image-20230829153528713](https://img-blog.csdnimg.cn/img_convert/73981d8c82d4bc6cd9a3a4def0598df5.png)
节点流和处理流
![image-20230829153657211](https://img-blog.csdnimg.cn/img_convert/f418cab0bdb822dc04ca200993cf5ced.png)
![image-20230829153718787](https://img-blog.csdnimg.cn/img_convert/8349b6e3c004eb4165f75c905879c25e.png)
![image-20230829153813306](https://img-blog.csdnimg.cn/img_convert/a5f1d01254aee7093ebdf3480de2565d.png)
处理流-BufferedReader 和BufferedWriter
![image-20230829154104779](https://img-blog.csdnimg.cn/img_convert/df388bf68f784f1d0097be4238b3c2b4.png)
处理流-BufferedInputStream 和BufferedOutputStream
![image-20230829154455855](https://img-blog.csdnimg.cn/img_convert/57ad1cff4a2cef19dc604f4b3a8201c3.png)
![image-20230829154635901](https://img-blog.csdnimg.cn/img_convert/4f10f0da2a6da1e66a30f896b48ca44f.png)
对象流-ObjectInputStream 和ObjectOutputStream
- 功能:提供了对基本类型或对象类型的序列化和反序列化的方法
- ObjectOutputStream 提供序列化功能
- ObjectInputStream 提供反序列化功能
![image-20230829154924383](https://img-blog.csdnimg.cn/img_convert/1fa2439ecbd0df54c7d3a9047dbc0527.png)
转换流-InputStreamReader 和OutputStreamWriter
![image-20230829155117608](https://img-blog.csdnimg.cn/img_convert/3f719cabd5c9a519558de420ef2b2512.png)
打印流-PrintStream 和PrintWriter
![image-20230829155246435](https://img-blog.csdnimg.cn/img_convert/3450b7996a2123e73ee81d0313edab00.png)
![image-20230829155306912](https://img-blog.csdnimg.cn/img_convert/4f6fb7e3042145f5c16574464f240c30.png)
![image-20230829155320211](https://img-blog.csdnimg.cn/img_convert/8f5b617cd573d8a3ae9d1f9c700cc498.png)
Properties 类
![image-20230829155504465](https://img-blog.csdnimg.cn/img_convert/8109543dec11dfb6385f9b51e35494e1.png)
![image-20230829155508167](https://img-blog.csdnimg.cn/img_convert/e969bd7a3012376b1e088696237bf3dd.png)