IO/File
1.FIle
代表的是文件或者目录(有可能不存在 当路径写错的时候)
文件路径
//路径的写法 绝对路径 相对路径 相对于当前项目所在路径 ./表示当前项目所在路径 ../返回到上级路径
File file1 = new File("./resource\\a.txt");
File file2 = new File("E:/io/Hello.txt");//Hello.txt
File file3 = new File("../klay");
System.out.println("文件或目录是否存在:"+file1.exists());
System.out.println(file2.exists());
System.out.println(file3.exists());
构造方法
/**
* File类构造方法
*/
public static void fileConstructorMethods() {
// 1, 传参是文件或者目录的 路径
File file = new File("./resources/a.txt");
// 2, 传参 父级路径 和子级路径
File file1 = new File("./resources", "a.txt");
// 创建父级对象
File file2 = new File("./resources");
// 3, 第三种构造 传父级对象 和子级路径
File file3 = new File(file2, "a.txt");
System.out.println("exists " + file3.exists());
}
常用方法
/**
* 常用方法
*/
public static void fileMethods() {
File file = new File("./resources/dir2/dir23/dir231");
System.out.println("是否存在 " + file.exists());
// 文件的字节大小
System.out.println("文件大小 " + file.length());
System.out.println("是否为文件 " + file.isFile());
System.out.println("是否为目录 " + file.isDirectory());
System.out.println("文件/目录 名称" + file.getName());
System.out.println("绝对路径: " + file.getAbsolutePath());
// 但是 目录下面没有子级的时候才会删除
// System.out.println("f删除文件/目录 " + file.delete());
// mkdir只能创建单级目录
System.out.println("创建目录 " + file.mkdir());
System.out.println("创建级联目录" + file.mkdirs());
/*try {
//当file代表的文件不存在的时候会创建新文件 否则不创建返回false
System.out.println("创建文件 " + file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}*/
}
获取文件列表
内部递归调用
ublic static void fileMethods1(File file, String ope) {
// 返回的是直接子系 3
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isFile()) {
System.out.println(ope + file1.getName());
} else {
//如果是目录的话
System.out.println(ope + file1.getName());
//递归调用
fileMethods1(file1, ope + " |");
}
}
}
测试类
public static void main(String[] args) {
File file = new File("./resources");
fileMethods1(file, " |");
}
获取文件列表时加过滤规则
File[] files = file.listFiles((path) -> {
if (path.isDirectory()) {
return true;
}
return false;
});
System.out.println(files.length);
2.IO
I:input 输入 O:output 输出 (在程序角度看 是输入还是输出)
按流向分为 输入流 输出流
按处理数据的基本单位不同 字节流 字符流(字符编码 / 字符解码)
1.字节流(InputStream/OutputStream)
构造方法
public static void inputStreamMethod() {
try {
FileInputStream fileInputStream = new FileInputStream("./resources/a.txt");
File file = new File("./resources/a.txt");
FileInputStream fileInputStream1 = new FileInputStream(file);
System.out.println(fileInputStream);
} catch (FileNotFoundException e) {
// 打印详细的堆栈报错信息
e.printStackTrace();
System.out.println(e.getMessage());
}
}
读取文件内容
public static void inputStreamMethod1() {
// try新语法种只能放实现了Closeable的实现类,会自动调用其close方法
try (FileInputStream fileInputStream = new FileInputStream("./resources/a.txt")) {
int num;
// 返回值是-1的时候表示读到文件末尾了 读完了
while ((num = fileInputStream.read()) != -1) {
System.out.println("数据 " + num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2.字节输出流FileOutputStream
write()方法
/**
*文件输出流
*/
public static void outputStreamMethod() {
// 输出流会自动创建文件(如果文件不存在) 构造方法中 true表示追加在文件尾部继续添加
try (FileOutputStream fileOutputStream = new FileOutputStream("./resources/b.txt")) {
fileOutputStream.write(97);
fileOutputStream.write(98);
fileOutputStream.write(99);
fileOutputStream.write(100);
fileOutputStream.write("程序".getBytes());
// fileOutputStream.write(101);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
字节流实现文件包拷贝
/**
*图片拷贝
*/
public static void fileCopyMethod() {
try (FileInputStream fileInputStream = new FileInputStream("./resources/sl.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("./resources/sl2.jpg")) {
int num;
while ((num = fileInputStream.read()) != -1) {
fileOutputStream.write(num);
}
System.out.println("copy finished");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3.带缓冲区的字节流
实现文件拷贝
public static void copyBuffer() {
//高效缓存流
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("./resource/dir2/2.jpg"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("./resource/dir2/dir5/3.jpg"))) {
int num;
while ((num = bufferedInputStream.read()) != -1) {
//只是把数据放在了数组里,当数组元素满了或者调用了close()方法的时候开始真的写入
bufferedOutputStream.write(num);
}
System.out.println("拷贝成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4.字符流
子字节流的基础上 加上了字符的编码和解码
常用来处理文字文本
字符流不能处理二进制文件(图片 视频 音频 只能使用字节流处理)
5文件字符流
public static void readerMethod() {
try (FileReader fileReader = new FileReader("./resources/a.txt");
FileWriter fileWriter = new FileWriter("./resources/test.txt")) {
int num;
fileWriter.write("hello world");
char[] chars = "你好".toCharArray();
fileWriter.write(chars);
/*while ((num = fileReader.read()) != -1) {
fileWriter.write(num);
// System.out.println(((char) num));
}*/
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
6.高效字符流
public static void bufferReaderMethod() {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader("./resources/a.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("./resources/c.txt"))) {
String str;
//一次读取一行 当读到流尾部的时候返回null
/*while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}*/
bufferedWriter.write("aaa");
bufferedWriter.write("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
7.转换流
- 把字节输入流转为字符输入流
- 把字符输出流转为字节输出流
public static void changeStreamMethod() {
// InputStreamReader把字节输入流转成字符输入流
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String str;
while (!(str = bufferedReader.readLine()).equals("quit")) {
System.out.println(str);
}
System.out.println("退出了");
} catch (IOException e) {
e.printStackTrace();
}
}
转换流
public static void changeStreamMethod1() {
//
try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out))) {
bufferedWriter.write("hello");
bufferedWriter.newLine();
bufferedWriter.write("bye bye");
} catch (IOException e) {
e.printStackTrace();
}
}
8.对象序列化
- 对象的序列化 把内存中的对象写入到底层存储介质中
- 对象的反序列化 把底层存储介质中数据恢复到内存中
- 注意点 对象类型要实现接口 Serializable 是个标记接口 给JVM看的,真正的序列化技术 由JVM来实现
- 其中的成员变量类型必须都是可序列化的
- transient用来修饰成员变量表示不对此成员变量进行序列化;那么在反序列化的时候会得到的是其数据类型的零值;
- 自定义类型中添加versionID 如果序列化之后源码发生了改动那么反序列化就会失败 因为版本不一致
- 类的定义
public class Student implements Serializable {
private static final long serialVersionUID = 5404764234087337539L;
private String name;
private Integer age;
private transient Car car;
//trandient表示该成员变量不被序列化
public Student() {
}
public Student(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
/* @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}*/
}
测试类
public class SeriaExercise {
public static void main(String[] args) {
// seMethod();
unSeMethod();
}
public static void unSeMethod() {
try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("./resources/obj.txt"))) {
//反序列化
Object object = objectInputStream.readObject();
if (object instanceof Student) {
Student student = (Student) object;
System.out.println("name " + student.getName());
System.out.println("age " + student.getAge());
System.out.println("car "+student.getCar());
}
// System.out.println(object);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 对象的序列化 把内存中的对象写入到底层存储介质中
* 注意点 对象类型要实现接口 Serializable
*/
public static void seMethod() {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("./resources/obj.txt"))) {
Student tom = new Student("tom", 12, new Car());
//对象序列化
objectOutputStream.writeObject(tom);
System.out.println(" 序列化完成 ");
} catch (IOException e) {
e.printStackTrace();
}
}
}
9.数据流
- 操作基本数据类型的流对象
- 先写后读
- 写入顺序和读取顺序保持一致
写入数据
public static void dataIoMethod() {
try (DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("./resources/data.txt"))) {
dataOutputStream.writeInt(100); // int 4
dataOutputStream.writeLong(100L);// 8
dataOutputStream.writeUTF("java");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
读出数据
public static void dataIoMethod1() {
try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("./resources/data.txt"))) {
int i = dataInputStream.readInt();
long v = dataInputStream.readLong(); // 4
String s = dataInputStream.readUTF();
System.out.println(" int " + i);
System.out.println(" long " + v);
System.out.println(" str " + s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}