IO流概述 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中 IO流分类 按照数据流向 输入流 读入数据 输出流 写出数据 按照数据类型 字节流 可以读写任何类型的文件 比如音频 视频 文本文件 字符流 只能读写文本文件 什么情况下使用哪种流呢? 如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。 如果你什么都不知道,就用字节流 IO流基类概述 a:字节流的抽象基类: InputStream ,OutputStream。 b:字符流的抽象基类: Reader , Writer。 注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。 如:InputStream的子类FileInputStream。 如:Reader的子类FileReader。 FileOutputStream的构造方法, FileOutputStream(File file) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流 FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 FileOutputStream(FileDescriptor fdObj) 创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件 系统中的某个实际文件的现有连接。 FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流。 FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流。 使用具体子类FileOutputStream Io流的分类: - (1): 按照流向进行划分 输入流 输出流

  • (2): 按照操作的数据类型进行划分
  • 字节流
  • 字节输入流 InputStream 读
  • 字节输出流 OutputStream 写
  • 字符流
  • 字符输入流 Reader 读
  • 字符输出流 Writer 写 注意事项: 创建字节输出流对象了做了几件事情? a:调用系统资源创建a.txt文件 b:创建了一个fos对象 c:把fos对象指向这个文件 为什么一定要close()? a: 通知系统释放关于管理a.txt文件的资源 b: 让Io流对象变成垃圾,等待垃圾回收器对其回收 FileInputStream 构造方法:FileInputStream(File file) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中 的 File 对象 file 指定。 FileInputStream(FileDescriptor fdObj) 通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统 中某个实际文件的现有连接。 FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中 的路径名 name 指定。 BufferedOutputStream A:缓冲思想 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多, 这是加入了数组这样的缓冲区效果,java本身在设计的时候, 也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流 B:BufferedOutputStream的构造方法 BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。 BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。 BufferedInputStream A:BufferedInputStream的构造方法 BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 BufferedInputStream(InputStream in, int size) 创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便 将来使用。 具体案例演示 基本字节流一次读写一个字节 public class t7 { public static void main(String[] args) throws IOException { FileInputStream inputStream = new FileInputStream("Student.txt"); FileOutputStream outputStream = new FileOutputStream("sss.txt"); int num=0; while ((num=inputStream.read())!=-1){ outputStream.write(num); } inputStream.close(); outputStream.close(); } } 基本字节流一次读写一个字节数组 public class t8 { public static void main(String[] args) throws IOException { FileInputStream inputStream = new FileInputStream("Student.txt"); FileOutputStream outputStream = new FileOutputStream("sss.txt"); int num=0; byte[] bytes = new byte[1024 * 1024]; while ((num=inputStream.read(bytes))!=-1){ outputStream.write(bytes,0,num); } inputStream.close(); outputStream.close(); } } 高效字节流一次读写一个字节 public class t9 { public static void main(String[] args) throws IOException { BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Student.txt")); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("sss.txt")); int num=0; while ((num=inputStream.read())!=-1){ outputStream.write(num); } inputStream.close(); outputStream.close(); } } 高效字节流一次读写一个字节数组 public class t10 { public static void main(String[] args)throws IOException { BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Student.txt")); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("sss.txt")); int num=0; byte[] bytes = new byte[1024 * 1024]; while ((num=inputStream.read(bytes))!=-1){ outputStream.write(bytes,0,num); } inputStream.close(); outputStream.close(); } }

字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。

字符流: 字符流 = 字节流 + 编码表 编码: 就是把字符串转换成字节数组

  • 把一个字符串转换成一个字节数组
  • public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
  • public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
  • 解码: 把字节数组转换成字符串
  • public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
  • public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
  • 使用什么字符集进行编码,那么就是使用什么字符集进行解码
  • 老地方 ----- 十进制 ---- 二进制 ---- 发出去
  • 接收 ---- 二进制 ---- 十进制 --- 老地方 编码:把看得懂的变成看不懂的: String -- byte[] 解码:把看不懂的变成看得懂的: byte[] -- String OutputStreamWriter OutputStreamWriter的构造方法 OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流 OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流 方法概述 public void write(int c) 写一个字符 public void write(char[] cbuf) 写一个字符数组 public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分 public void write(String str) 写一个字符串 public void write(String str,int off,int len) 写一个字符串的一部分 InputStreamReader InputStreamReader的构造方法 InputStreamReader(InputStream is):用默认的编码(GBK)读取数据 InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据 方法概述 public int read() 一次读取一个字符 public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1 FileWriter和FileReader FileReader和FileWriter的出现 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的, 所以,为了简化我们的书写,转换流提供了对应的子类。 FileWriter FileReader 字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集, 于是java就给我们提供转换流对应的便捷类 转换流 -------------------------- 便捷类 OutputStreamWriter ------- FileWriter InputStreamReader ------- FileReader 字符缓冲流的特殊功能 BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符 BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 字符流复制文件 基本的流一次一个字符 public class t1 { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt")); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("SSS.txt")); int len=0; while ((len=reader.read())!=-1){ writer.write(len); writer.flush(); } writer.close(); reader.close(); } } 基本的流一次一个字符数组 public class t2 { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt")); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("sss.txt")); int len=0; char[] chars = new char[1024 * 1024]; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); writer.flush(); } reader.close(); writer.close(); } } 高效的流一次一个字符 public class t3 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); int len=0; while ((len=reader.read())!=-1){ writer.write(len); writer.flush(); } reader.close(); writer.close(); } }

高效的流一次一个字符数组 public class t4 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); int len=0; char[] chars = new char[1024 * 1024]; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); writer.flush(); } reader.close(); writer.close(); } } 高效的流一次一行字符串 public class t5 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); String len=null; while ((len=reader.readLine())!=null){ writer.write(len); writer.newLine(); writer.flush(); } reader.close(); writer.close();

}

} IO流(键盘录入学生信息按照总分排序并写入文本文件) public class Student implements Comparable<Student>{ private String name; private int chinese; private int math; private int English; private int all;

public Student() {
}

public Student(String name, int chinese, int math, int english, int all) {
    this.name = name;
    this.chinese = chinese;
    this.math = math;
    English = english;
    this.all = all;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getChinese() {
    return chinese;
}

public void setChinese(int chinese) {
    this.chinese = chinese;
}

public int getMath() {
    return math;
}

public void setMath(int math) {
    this.math = math;
}

public int getEnglish() {
    return English;
}

public void setEnglish(int english) {
    English = english;
}

public int getAll() {
    return all;
}

public void setAll(int all) {
    this.all = all;
}

@Override
public String toString() {
    return "学生: " + "姓名~┏" + name + '┒' +
            ", 语文~" + chinese +
            ", 数学~" + math +
            ", 英语~" + English +
            ",总分~" + all ;
}

@Override
public int compareTo(Student student) {
    int num=-(this.all-student.all);
    int num2=num==0?this.name.compareTo(student.name):num;
    return num2;
}

}

public class text3 { /* 3.A:案例演示: 需求:键盘录入3个学生信息( 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),*/ public static void main(String[] args) throws IOException { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Student.txt")); Scanner scanner = new Scanner(System.in); TreeSet<Student> students = new TreeSet<>(); for (int i = 0, n = 1; i < 3; i++, n++) { Student student = new Student(); System.out.println("请输入第" + n + "个学生的姓名"); String name = scanner.nextLine(); student.setName(name); System.out.println("请输入第" + n + "个学生的语文成绩"); int chinese = scanner.nextInt(); student.setChinese(chinese); System.out.println("请输入第" + n + "个学生的数学成绩"); int math = scanner.nextInt(); student.setMath(math); System.out.println("请输入第" + n + "个学生的英语成绩"); int English = scanner.nextInt(); student.setEnglish(English); scanner = new Scanner(System.in); int num = chinese + math + English; student.setAll(num); students.add(student); }

    for (Student s : students) {
        String s1 = String.valueOf(s);
        out.write(s1.getBytes());
        out.write("\n\r".getBytes());
    }
    out.close();

}

}