|
@@ -0,0 +1,114 @@
|
|
|
+package com.sf.javase.io;
|
|
|
+
|
|
|
+import lombok.SneakyThrows;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件的拷贝 a.txt -> b.txt
|
|
|
+ * 从a中读 往b中写
|
|
|
+ */
|
|
|
+public class StreamMain {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // File 是找到这个文件
|
|
|
+// File file = new File("/Users/Qing/Projects/IdeaProjects/SiFu/VIP31/springboot-demo/some.txt");
|
|
|
+// // 是不是文件
|
|
|
+// System.out.println(file.isFile());
|
|
|
+
|
|
|
+// File file1 = new File("/Users/Qing/Projects/IdeaProjects/SiFu/VIP31/springboot-demo");
|
|
|
+// // 是不是文件夹
|
|
|
+// System.out.println(file1.isDirectory());
|
|
|
+
|
|
|
+ // 在获取文件时 以当前项目作为根路径 来文件的拼接相对路径
|
|
|
+ File file = new File("data/some.txt");
|
|
|
+ System.out.println(file.getAbsolutePath());
|
|
|
+
|
|
|
+// readFile(file);
|
|
|
+// readFile1(file);
|
|
|
+// readFile2(file);
|
|
|
+// readFile3(file);
|
|
|
+ writeFile(file,"0123456789");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void readFile(File file) {
|
|
|
+ // IO流是读写文件
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ while (true) {
|
|
|
+ // 循环调用read()直到返回-1
|
|
|
+ int n = inputStream.read();
|
|
|
+ if (n == -1) break;
|
|
|
+ // 通过字节数组接收数据
|
|
|
+ byte[] bytes = new byte[1];
|
|
|
+ bytes[0] = (byte) n;
|
|
|
+ System.out.println(new String(bytes));
|
|
|
+ }
|
|
|
+ // 流是资源 资源需要手动关闭
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void readFile1(File file) {
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ // 创建一个和文件长度相等的字节数组
|
|
|
+ byte[] bytes = new byte[(int) file.length()];
|
|
|
+ // 将流中的数据读入到字节数组中
|
|
|
+ inputStream.read(bytes);
|
|
|
+ System.out.println(new String(bytes));
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 在一个一个读取和直接全部读取之间 使用批量的按照指定大小来读取
|
|
|
+ @SneakyThrows
|
|
|
+ public static void readFile2(File file) {
|
|
|
+ // 文件中的数据都进入了inputstream 输入流
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ // 创建一定长度的字节数组作为缓冲
|
|
|
+ byte[] bytes = new byte[16];
|
|
|
+ // 记录读取的次数
|
|
|
+ int cnt = 0;
|
|
|
+ // 将流中的数据循环读入到字节数组中
|
|
|
+ while (true) {
|
|
|
+ // 使用read(读取出的数据要存放的字节数组,偏移量,预读取长度)
|
|
|
+ // 返回的是实际读取的长度
|
|
|
+ // 123456789123456789123456789
|
|
|
+ // 第一次读取 想要16 返回16 1234567891234567
|
|
|
+ // 第二次读取 想要16 返回11 89123456789
|
|
|
+ // 第三次读取 想要16 返回-1
|
|
|
+ int len = inputStream.read(bytes, 0, bytes.length);
|
|
|
+ cnt++;
|
|
|
+ if (len == -1) break;
|
|
|
+
|
|
|
+ // 用String的类似构造器得到字符串
|
|
|
+ System.out.println(new String(bytes, 0, len));
|
|
|
+ }
|
|
|
+ System.out.println(cnt);
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void readFile3(File file) {
|
|
|
+ // IO流是读写文件
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ byte[] bytes = new byte[16];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = inputStream.read(bytes)) != -1) {
|
|
|
+ System.out.println(new String(bytes, 0, len));
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ // 写数据 文件本身 和 文件内容
|
|
|
+ public static void writeFile(File file, String content) {
|
|
|
+ // 把内容转化为字节数组
|
|
|
+ // 把文件转为输出流
|
|
|
+ // 把字节数组写入到输出流
|
|
|
+ byte[] bytes = content.getBytes();
|
|
|
+ OutputStream outputStream = new FileOutputStream(file);
|
|
|
+ outputStream.write(bytes);
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|