IO框架

1. File类

File类有用于操作文件以及查看文件属性的方法

  1. package com.qfedu.test1;
  2. import java.io.File;
  3. import java.io.IOException;
  4. /**
  5. * File类 文件操作类 可以查看文件的属性 和 创建文件等
  6. * @author WHD
  7. *
  8. */
  9. public class TestFile {
  10. public static void main(String[] args) throws IOException {
  11. File file2 = new File("C:\\Users\\WHD\\Desktop\\a.txt");
  12. System.out.println(file2.createNewFile());
  13. File file1 = new File("a.txt");
  14. System.out.println(file1.createNewFile());
  15. System.out.println("是否是一个文件" + file1.isFile());
  16. System.out.println("是否是一个文件夹" + file1.isDirectory());
  17. System.out.println("相对路径" + file1.getPath());
  18. System.out.println("绝对路径" + file1.getAbsolutePath());
  19. System.out.println("文件名称" + file1.getName());
  20. System.out.println("文件大小" + file1.length());
  21. System.out.println("文件是否存在" + file1.exists());
  22. System.out.println("文件删除是否成功" + file1.delete());
  23. File file3 = new File("A");
  24. file3.mkdir();// make directory
  25. File file4 = new File("B/C/D");
  26. file4.mkdirs();
  27. file3.delete();
  28. file4.delete();
  29. }
  30. }

2. 字节流(读取)

2.1 InputStream

字节读取流父类 抽象类

2.2 FileInputStream

InputStream字节读取流父类 FileInputStream 子类 read() 每次读取一个字节 返回值为读取的内容 read(byte [] data) 返回值为读取的个数 读取到的内容存在数组中 close() 关闭资源

  1. package com.qfedu.test2;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. /**
  6. * InputStream字节读取流父类
  7. * FileInputStream 子类
  8. * read() 每次读取一个字节 返回值为读取的内容
  9. * read(byte [] data) 返回值为读取的个数 读取到的内容存在数组中
  10. * close() 关闭资源
  11. * @author WHD
  12. *
  13. */
  14. public class TestFileInputStream1 {
  15. public static void main(String[] args) {
  16. try {
  17. FileInputStream fis = new FileInputStream("A.txt");
  18. int data = fis.read();
  19. System.out.println("第一次读取" + (char)data);
  20. int readData = -1;
  21. while((readData = fis.read()) != -1) {
  22. System.out.println((char)readData);
  23. }
  24. } catch (FileNotFoundException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }

3.字节流(写入)

3.1 OutputStream

字节写入流父类 抽象类

3.2 FileOutputStream

OutputStream FileOutputStream write(int data); 写一个字节 write(byte [] data); 写一个字节数组 close()

  1. package com.qfedu.test4;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. /**
  6. * OutputStream
  7. * FileOutputStream
  8. * write(int data); 写一个字节
  9. * write(byte [] data); 写一个字节数组
  10. * close()
  11. * @author WHD
  12. *
  13. */
  14. public class TestFileOutputStream {
  15. public static void main(String[] args) {
  16. FileOutputStream fos = null;
  17. try {
  18. fos = new FileOutputStream("B.txt");
  19. fos.write(97);
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }finally {
  25. try {
  26. if(fos != null) {
  27. fos.close();
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }

4. 字符流(读取)

4.1 Reader

字符读取流父类 抽象类

4.1.1 InputStreamReader

可以指定读取编码格式的字符读取类 read() 每次读取一个字符

  1. package com.qfedu.test5;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. /**
  7. * Reader 字符流读取父类 抽象类
  8. * InputStreamReader 子类
  9. * read() 每次读取一个字符
  10. * @author WHD
  11. *
  12. */
  13. public class TestInputStreamReader {
  14. public static void main(String[] args) {
  15. FileInputStream fis = null;
  16. InputStreamReader isr = null;
  17. try {
  18. fis = new FileInputStream("A.txt");
  19. isr = new InputStreamReader(fis);
  20. int data = -1;
  21. while((data = isr.read())!= -1) {
  22. System.out.println((char)data);
  23. }
  24. } catch (FileNotFoundException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }finally {
  29. try {
  30. if(fis != null) {
  31. fis.close();
  32. }
  33. if(isr != null) {
  34. isr.close();
  35. }
  36. }catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }

4.1.1.1 FileReader

只能按照本地默认的编码格式读取文件 字符流

  1. package com.qfedu.test6;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. /**
  6. * Reader
  7. * InputStreamReader
  8. * FileReader
  9. * @author WHD
  10. *
  11. */
  12. public class TestFileReader {
  13. public static void main(String[] args) {
  14. try {
  15. FileReader fr = new FileReader("A.txt");
  16. int data = -1;
  17. while(( data = fr.read()) != -1) {
  18. System.out.println((char)data);
  19. }
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }finally {
  25. // 关闭资源
  26. }
  27. }
  28. }

4.1.2 BufferedReader

独有的方法 readLine() 读取一行

  1. package com.qfedu.test7;
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. /**
  7. * Reader
  8. * BufferedReader
  9. *
  10. * readLine() 读取一行
  11. * @author WHD
  12. *
  13. */
  14. public class TestBufferedReader1 {
  15. public static void main(String[] args) throws IOException {
  16. FileInputStream fis = new FileInputStream("A.txt");
  17. InputStreamReader isr = new InputStreamReader(fis);
  18. BufferedReader br = new BufferedReader(isr);
  19. String line = null;
  20. while((line = br.readLine()) != null) {
  21. System.out.println(line);
  22. }
  23. // 关闭资源
  24. }
  25. }

5. 字符流(写入)

5.1 Writer

字符写入流父类 抽象类

5.1.1 OutputStreamWriter

write(String str) 可以直接写入字符串 可以指定写入的编码格式

  1. package com.qfedu.test8;
  2. import java.io.IOException;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. /**
  6. * Writer
  7. * OutputStreamWriter
  8. * @author WHD
  9. *
  10. */
  11. public class TestOutputStreamWriter1 {
  12. public static void main(String[] args) throws IOException {
  13. OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D.txt",true),"utf-8");
  14. osw.write("hello world世界你好");
  15. osw.close();
  16. }
  17. }

5.1.1.1 FileWriter

只能按照本地的默认的编码格式写入文件

  1. package com.qfedu.test8;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. /**
  5. * Writer
  6. * OutputStreamWriter
  7. * FileWriter
  8. * @author WHD
  9. *
  10. */
  11. public class TestFileWriter1 {
  12. public static void main(String[] args) {
  13. FileWriter fw;
  14. try {
  15. fw = new FileWriter("E.txt");
  16. fw.write("abcdefg");
  17. fw.flush(); // 刷新 表示将内容从内存刷新到硬盘
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }finally {
  21. // 关闭资源
  22. }
  23. }
  24. }

5.1.2 BufferedWriter

独有的newLine() 换行的方法

  1. package com.qfedu.test8;
  2. import java.io.BufferedWriter;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. /**
  8. * Writer
  9. * BufferedWriter
  10. * @author WHD
  11. *
  12. */
  13. public class TestBufferedWriter1 {
  14. public static void main(String[] args) throws IOException {
  15. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F.txt")));
  16. bw.write("abcd\n");
  17. bw.write("efgh");
  18. bw.newLine();
  19. bw.write("hello world");
  20. bw.flush();
  21. }
  22. }

6. 数据流

用于读写二进制文件 DataInputStream读取二进制文件 DataOutputStream写入二进制文件

  1. package com.qfedu.test9;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. /**
  8. * DataInputStream读取二进制文件
  9. * DataOutputStream写入二进制文件
  10. *
  11. * @author WHD
  12. *
  13. */
  14. public class TestDataStream {
  15. public static void main(String[] args) throws IOException {
  16. FileInputStream fis = new FileInputStream("C:\\Users\\WHD\\Desktop\\zs1.jpg");
  17. DataInputStream dis = new DataInputStream(fis);
  18. byte data [] = new byte[fis.available()];
  19. dis.read(data);
  20. // System.out.println(new String(data,0,data.length));
  21. DataOutputStream dos = new DataOutputStream(new FileOutputStream("copyzs.jpg"));
  22. dos.write(data);
  23. fis.close();
  24. dis.close();
  25. dos.close();
  26. }
  27. }

7. 序列化

序列化:将对象写入到二进制文件中 反序列化:将存有对象的二进制文件读取为对象 要求:被序列化的对象所属类必须实现Serializable接口 此接口是空接口 相当于一个标识 被transient修饰的属性不能被序列化

  1. package com.qfedu.test9;
  2. import java.io.Serializable;
  3. public class Student implements Serializable{
  4. private static final long serialVersionUID = -4543464400965999994L;
  5. private String name;
  6. private int age;
  7. private transient String address;
  8. public String getAddress() {
  9. return address;
  10. }
  11. public void setAddress(String address) {
  12. this.address = address;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public int getAge() {
  21. return age;
  22. }
  23. public void setAge(int age) {
  24. this.age = age;
  25. }
  26. public Student() {
  27. }
  28. public Student(String name, int age) {
  29. this.name = name;
  30. this.age = age;
  31. }
  32. public Student(String name, int age, String address) {
  33. this.name = name;
  34. this.age = age;
  35. this.address = address;
  36. }
  37. @Override
  38. public String toString() {
  39. return "Student [name=" + name + ", age=" + age + ", address=" + address + "]";
  40. }
  41. }
  1. package com.qfedu.test9;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. /**
  8. * DataInputStream读取二进制文件
  9. * DataOutputStream写入二进制文件
  10. *
  11. * @author WHD
  12. *
  13. */
  14. public class TestDataStream {
  15. public static void main(String[] args) throws IOException {
  16. FileInputStream fis = new FileInputStream("C:\\Users\\WHD\\Desktop\\zs1.jpg");
  17. DataInputStream dis = new DataInputStream(fis);
  18. byte data [] = new byte[fis.available()];
  19. dis.read(data);
  20. // System.out.println(new String(data,0,data.length));
  21. DataOutputStream dos = new DataOutputStream(new FileOutputStream("copyzs.jpg"));
  22. dos.write(data);
  23. fis.close();
  24. dis.close();
  25. dos.close();
  26. }
  27. }