一、字节高级流(处理流)

高级流就是针对低级流的封装,并提供了更高的功能

1.1 字节高级流的分类

过滤流(*)

过滤流,针对低级流做了个过滤处理

缓冲流 BufferInputStream BufferOutPutStream

缓冲流主要功能是:为低级流在读写时,提供一种缓冲能力(靠内存)

数据流 DataInputStream DataOutPutStream

数据流主要功能是:允许我们使用一种与计算机无关的格式去读写数据

打印输出流 PrintStream

它允许字符串流中,带有自动刷新功能

对象流(*)

对象流主要功能是:允许程序使用整个对象来做数据的读写

合并流(了解)

SequenceInputStream类可以实现两个文件的合并操作,从两个低级流中读取数据字节,当到达流的末尾时从一个流转到另一个流 ,从而实现合并操作

1.2 字节输入缓冲流的使用

QQ截图20201013142251.png

  1. package com.woniuxy.java18.study.advance;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. /**
  7. * 字节输入缓冲流
  8. * @author Administrator
  9. *
  10. */
  11. public class BufferInputStreamStudy {
  12. public static void main(String[] args) {
  13. // TODO Auto-generated method stub
  14. //定义一个文件对象
  15. String path = "D:\\movie\\japan\\dj\\bdy.txt";
  16. File file = new File(path);
  17. //定义缓冲输入流的变量
  18. BufferedInputStream bis = null;
  19. FileInputStream in = null;
  20. try {
  21. //定义文件输入流,并将其进行Buffer包装
  22. in = new FileInputStream(file);
  23. bis = new BufferedInputStream(in);
  24. byte[] data = new byte[1024];
  25. //读取数据
  26. while(bis.read(data) != -1) {
  27. //将接收到的内容,输出到控制台
  28. System.out.println(new String(data,"utf-8"));
  29. }
  30. }catch (Exception e) {
  31. // TODO: handle exception
  32. e.printStackTrace();
  33. }finally {
  34. //关闭流
  35. try {
  36. bis.close();
  37. in.close();
  38. } catch (IOException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }

1.3 字节输出缓冲流的使用

QQ截图20201013142251.png

  1. package com.woniuxy.java18.study.advance;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. /**
  7. * 字节输出缓冲流
  8. * @author Administrator
  9. *
  10. */
  11. public class BufferOutputStreamStudy {
  12. public static void main(String[] args) {
  13. // TODO Auto-generated method stub
  14. //定义文件对象
  15. String path = "D:\\movie\\japan\\dj\\cxk.txt";
  16. File file = new File(path);
  17. BufferedOutputStream bos = null;
  18. FileOutputStream fos = null;
  19. try {
  20. if(!file.exists()) {
  21. file.createNewFile();
  22. }
  23. //实例化流
  24. fos = new FileOutputStream(file);
  25. bos = new BufferedOutputStream(fos);
  26. //向文件写入内容
  27. String str = "它不是拍电影的,它是个运动员!!!!!!!";
  28. byte[] data = str.getBytes();
  29. //写
  30. bos.write(data);
  31. //刷新(清空)缓冲流-->低级流
  32. bos.flush();
  33. }catch (Exception e) {
  34. // TODO: handle exception
  35. e.printStackTrace();
  36. }finally {
  37. //关闭流
  38. try {
  39. bos.close();
  40. fos.close();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }

1.4 数据流 DataInputStream DataOutputStream

这2个流,主要数据流,它的作用:可以让我们采用 与计算机无法的方式来完成对文件进行读写
与计算机无法的方式:采用Java的数据类型(String,基本数据类型)进行文件内容操作

  1. package com.woniuxy.java18.study.advance;
  2. import java.io.BufferedInputStream;
  3. import java.io.DataInputStream;
  4. import java.io.EOFException;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.util.Arrays;
  9. /**
  10. * 数据输入流(高级流)
  11. * 特点:允许代码使用一种与计算机无法的格式来进行内容输出
  12. * @author Administrator
  13. *
  14. */
  15. public class DataInputStreamStudy {
  16. public static void main(String[] args) {
  17. // TODO Auto-generated method stub
  18. File file = new File("D:\\94b69bb9-ea80-4a08-ade9-48a1a0bcbaf8.txt");
  19. DataInputStream dis = null;
  20. FileInputStream fis = null;
  21. BufferedInputStream bis = null;
  22. User[] users = new User[2];
  23. int index = 0;
  24. try {
  25. fis = new FileInputStream(file);
  26. bis = new BufferedInputStream(fis);
  27. //不考虑缓冲流,就直接使用文件输入流
  28. // dis = new DataInputStream(fis);
  29. dis = new DataInputStream(bis);
  30. //操作流
  31. while(true) {
  32. String str = dis.readUTF();
  33. int age = dis.readInt();
  34. boolean marry = dis.readBoolean();
  35. double income = dis.readDouble();
  36. User user = new User(str, age, marry, income);
  37. users[index] = user;
  38. index ++;
  39. }
  40. } catch (Exception e) {
  41. // TODO: handle exception
  42. if(e instanceof EOFException) {
  43. System.out.println("文件读取完毕!!!!");
  44. }else {
  45. e.printStackTrace();
  46. }
  47. }finally {
  48. System.out.println(Arrays.toString(users));
  49. try {
  50. dis.close();
  51. bis.close();
  52. fis.close();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  1. package com.woniuxy.java18.study.advance;
  2. public class User {
  3. private String name;
  4. private int age;
  5. private boolean marry;
  6. private double income;
  7. public User() {
  8. super();
  9. // TODO Auto-generated constructor stub
  10. }
  11. public User(String name, int age, boolean marry, double income) {
  12. super();
  13. this.name = name;
  14. this.age = age;
  15. this.marry = marry;
  16. this.income = income;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. public boolean isMarry() {
  31. return marry;
  32. }
  33. public void setMarry(boolean marry) {
  34. this.marry = marry;
  35. }
  36. public double getIncome() {
  37. return income;
  38. }
  39. public void setIncome(double income) {
  40. this.income = income;
  41. }
  42. @Override
  43. public String toString() {
  44. return "User [name=" + name + ", age=" + age + ", marry=" + marry + ", income=" + income + "]";
  45. }
  46. }
  1. package com.woniuxy.java18.study.advance;
  2. import java.io.BufferedOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.UUID;
  8. /**
  9. * 数据输出流(高级流)
  10. * 特点:允许代码使用一种与计算机无法的格式来进行内容输出
  11. * @author Administrator
  12. *
  13. */
  14. public class DataOutPutStreamStudy {
  15. public static void main(String[] args) {
  16. // TODO Auto-generated method stub
  17. File file = new File("D:\\" + UUID.randomUUID().toString()+".txt");
  18. DataOutputStream dos = null;
  19. //fos 必写
  20. FileOutputStream fos = null;
  21. //bos 选写(看你是否看重 IO性能)
  22. BufferedOutputStream bos = null;
  23. try {
  24. if(!file.exists()) {
  25. file.createNewFile();
  26. }
  27. //实例化流
  28. fos = new FileOutputStream(file);
  29. bos = new BufferedOutputStream(fos);
  30. dos = new DataOutputStream(bos);
  31. //操作流(采用UTF-8写字符串,写基本数据类型)
  32. dos.writeUTF("我的爱人啊,你在哪里啊,我做梦都在想你!!!!");
  33. dos.writeInt(33);
  34. dos.writeBoolean(true);//写入boolean的值
  35. dos.writeDouble(1200000.0);
  36. //操作流(采用UTF-8写字符串,写基本数据类型)
  37. dos.writeUTF("我的爱人啊,你在哪里啊,我做梦都在想你!!!!");
  38. dos.writeInt(33);
  39. dos.writeBoolean(true);//写入boolean的值
  40. dos.writeDouble(1200000.0);
  41. } catch (Exception e) {
  42. // TODO: handle exception
  43. e.printStackTrace();
  44. }finally {
  45. try {
  46. dos.close();
  47. bos.close();
  48. fos.close();
  49. } catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }