IO-字符流&其他流

(1)字符流
  • 字符流
        字符输入流 Reader--FileReader
        字符输出流 Writer--FileWriter

  • FileReader
       1. 创建对象
            public FileReader(File file);
            public FileReader(string pathname);
       2. 读数据
            public int read() 一次读一个字符,返回字符对应的字,读到文件末尾返回-1
            public int read(char[] buffer) 一次读多个字符,返回本次读取的有效字节个数,读到文件末尾返回-1

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //创建对象
        FileReader reader = new FileReader("day09-code/a-1.txt");
        //io操作
        //定义一个变量
        int read;
        //while循环,读取一次返回值设置到变量 判断不等于-1
        while ((read = reader.read()) != -1) {
            System.out.println((char) read);
        }
        //关闭流
        reader.close();

    }
}

//    public static void main(String[] args) throws IOException {
//        FileReader reader = new FileReader("day09-code/a-1.txt");
//        char[] chars = new char[3];
//
//        int len;
//
//        while ((len = reader.read(chars))!=-1){
//            System.out.println("读取长度"+len+"内容"+new String(chars,0,len));
//        }
//        reader.close();
//    }
//}
1.字符流
    字符输入流 Reader--FileReader
    字符输出流 Writer--FileWriter

2.FileWriter
 *创建对象
   public FileWriter(File file)   创建字节输出流管道与源文件对象接通
   public FileWriter(String filepath) 创建字节输出流管道与源文件路径接通
   public FileWriter(File file,boolean append)    创建字节输出流管道与源文件对象接通,可追加数据
   public FileWriter(String filepath,boolean append)  创建字节输出流管道与源文件路径接通,可追加数据
 *写数据
        void write(int c)  写一个字符
        void write(String str) 写一个字符串
        void write(String str, int off, int len)   写一个字符串的一部分
        void write(char[] cbuf)    写入一个字符数组
        void write(char[] cbuf, int off, int len)  写入字符数组的一部分

注意:
    字符输出流写出数据后,必须刷新流(flush)或者关闭流(close),写出的数据才会生效,原因是有缓存区
    如果要换行, 需要输出"\r\n"字符串

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //1、创建文件输出流
        FileWriter writer = new FileWriter("day09-code/a-2.txt");

        //2、IO操作
        //写一个字符
        writer.write(97);
        //写入换行
        writer.write("\r\n");
        //写一个字符串
        writer.write("学it到黑马");
        writer.write("\r\n");
        //写一个字符串的一部分
        writer.write("我喜欢Java",3,4);//起始索引,长度
        writer.write("\r\n");
        //写入一个字符数组
        char [] chars = {'月','薪','过','万'};
        writer.write(chars);
        writer.write("\r\n");
        //写入字符数组的一部分
        writer.write(chars,2,2); //起始索引,长度

        //3、关闭流
        //writer.flush();
        writer.close(); //包含了flush
    }
}
(2)缓冲流 
  • 字节缓冲流
        自带8KB缓冲区, 减少了内存和硬盘之间的交互,从而提高原始流读、写数据的性能

  • 字节缓冲流
        public BufferedInputStream(InputStream is); 底层提供8K的缓冲区,减少和内存和硬盘的交互,真正干活的还是字节输入流
        public BufferedOutputStream(OutputStream os);底层提供8K的缓冲区,减少和内存和硬盘的交互,真正干活的还是字节输出流

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //1、创建输入和输出流
        FileInputStream fis = new FileInputStream("D:\\upload\\other\\duotai.wmv");
        BufferedInputStream bis = new BufferedInputStream(fis); //输入流

        FileOutputStream fos = new FileOutputStream("D:\\upload\\other\\duotai2.wmv");
        BufferedOutputStream bos = new BufferedOutputStream(fos); //输出流

        //2、IO操作
        int len;
        byte [] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }

        //3、关闭流
        bos.close();
        fos.close();
        bis.close();
        fis.close();
    }
}
//目标:观察原始流和缓冲流的性能。
public class Demo2 {
    // 复制的视频路径
    private static final String SRC_FILE = "D:/upload/other/duotai.wmv";
    // 复制到哪个目的地
    private static final String DEST_FILE = "D:/upload/other/duotai2.wmv";

    public static void main(String[] args) throws IOException {
        copyUseFileInputStream();//不使用缓冲流
        copyUseBufferedInputStream();//使用缓冲流
    }

    private static void copyUseFileInputStream() throws IOException {
        long startTime = System.currentTimeMillis();
        //1. 创建文件字节输入流、输出流
        FileInputStream fis = new FileInputStream(SRC_FILE);
        FileOutputStream fos = new FileOutputStream(DEST_FILE);

        //2. 使用输入流读取内容,使用输出流写出内容
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

        //3. 关闭流
        fos.close();
        fis.close();
        long endTime = System.currentTimeMillis();
        System.out.println("不使用缓冲流复制耗时:" + (endTime - startTime) + "ms");
    }


    private static void copyUseBufferedInputStream() throws IOException {
        long startTime = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(SRC_FILE));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(DEST_FILE));


        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        bis.close();
        bos.close();
        long endTime = System.currentTimeMillis();
        System.out.println("使用缓冲流复制耗时:" + (endTime - startTime)+ "ms");
    }


}
  •  字符缓冲流

       public BufferedReader(Reader r)    把低级的字符输入流包装成字符缓冲输入流管道,从而提高字符输入流读字符数据的性能
        public String readLine()   读取一行数据返回,如果没有数据可读了,会返回null

        public BufferedWriter(Writer r)    把低级的字符输出流包装成一个高级的缓冲字符输出流,从而提高字符输出流写数据的性能
            public void newLine()  换行

public class Demo3 {
    //使用字符缓冲流,将b-3.txt中的内容,写入b-4.txt中
    public static void main(String[] args) throws Exception {
        //1、创建对象
        BufferedReader reader = new BufferedReader(new FileReader("day09-code/b-3.txt"));
        BufferedWriter writer = new BufferedWriter(new FileWriter("day09-code/b-4.txt"));
        //2、IO操作

        //逐行读取内容
        String str;
        while ( (str = reader.readLine()) != null) {
            //逐行写入到新文件中
            writer.write(str);
            //换行
            writer.newLine();
        }

        //3、关闭流
        writer.close();
        reader.close();
    }

    //字符缓冲输出流
    public static void main2(String[] args) throws Exception {
        //1、创建对象
        BufferedWriter writer = new BufferedWriter(new FileWriter("day09-code/b-4.txt"));
        //2、IO操作
        writer.write("hello1");
        writer.newLine();
        writer.write("world");
        //3、关闭流
        writer.close();
    }

    //字符缓冲输入流
    public static void main1(String[] args) throws Exception {
        //读取b-3.txt的内容
        //1、创建BufferedReader对象
        BufferedReader reader = new BufferedReader(new FileReader("day09-code/b-3.txt"));
        //2、IO操作
        //2.1 定一个字符串变量
        String str;
        //2.2 while循环,读取一行,赋值变量。判断变量不等于null
        while ( (str = reader.readLine()) != null ) {
            System.out.println(str);
        }
        //3、关闭流
        reader.close();
    }
}
(3)转换流
问题描述:
    当程序使用的编码跟操作文档的编码不一致时,会出现乱码问题
    此时可以使用字符转换流
    它的思路是: 先获取文件的原始字节流,再将其按真实的字符集编码转成字符输入流,这样字符输入流中的字符就不乱码了
  •  字符输入转换流

       public InputStreamReader(InputStream is ,String charset) 把原始的字节输入流,按照指定字符集编码转成字符输入流
     

    public class Demo1 {
        public static void main(String[] args) throws Exception {
            //需求: 从c-1.txt读取内容
            //1、创建文件字节输入流
            FileInputStream fis = new FileInputStream("day09-code/c-1.txt");
            //2、创建一个字符输入转化流,并指定字符集
            InputStreamReader isr = new InputStreamReader(fis, "GBK");//fis,字符集
            //3、将字符输入转化流包装为字符输出缓冲流
            BufferedReader reader = new BufferedReader(isr);
            //4、读取一行
            String line = reader.readLine();
            System.out.println(line);
            //5、关闭
            reader.close();
            isr.close();
            fis.close();
        }
    }
    
    问题描述:
        当程序使用的编码跟操作文档的编码不一致时,会出现乱码问题
        此时可以使用字符转换流
        它的思路是: 获取字节输出流,再按照指定的字符集编码将其转换成字符输出流,以后写出去的字符就会用该字符集编码了。
    
  •  字符输出转换流

        public OutputStreamWriter(OutputStream os,String charset)  可以把原始的字节输出流,按照指定编码转换成字符输出流

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //需求: 向c-2.txt写出内容, 字符集为GBK
        //1、创建文件字节输出流
        FileOutputStream fos = new FileOutputStream("day09-code/c-2.txt");
        //2、创建字符输出转化流
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");//fos,字符集
        //3、创建缓冲字符输出流
        BufferedWriter writer = new BufferedWriter(osw);
        //4、写入数据
        writer.write("月薪过万");
        writer.newLine();
        writer.write("到黑马程序员");
        //5、关闭
        writer.close();
        osw.close();
        fos.close();
    }

}
  • JDK11开始,可直接使用字符流构造指定字符集,构造第二个参数,通过Charset的静态方法forName 指定字符集
        public FileReader(String fileName, Charset charset) throws IOException
        public FileWriter(String fileName, Charset charset) throws IOException

public class Demo3 {

    //缓冲字符输出流
    public static void main(String[] args) throws Exception {
        //缓冲字符输出流
        BufferedWriter writer = new BufferedWriter(new FileWriter("day09-code/c-2.txt", Charset.forName("GBK")));
        //向文件写入数据
        writer.write("好好学习,天天向上");
        //关闭
        writer.close();
    }


    //缓冲字符输入流
    public static void main1(String[] args) throws Exception {
        //1、缓冲字符输入流
        BufferedReader reader = new BufferedReader(new FileReader("day09-code/c-1.txt", Charset.forName("GBK")));
        //2、写入内容
        String line = reader.readLine();
        System.out.println(line);
        //3、关闭
        reader.close();
    }
}
(4)打印流
  • PrintStream/PrintWriter(打印流)
        打印流可以实现更方便、更高效的打印数据出去,能实现打印啥出去就是啥出去。

  • 构造器
        public PrintStream(OutputStream/File/String)   打印流直接通向字节输出流/文件/文件路径
        public PrintWriter(OutputStream/Writer/File/String) 打印流直接通向字符输出流/文件/文件路径

  • 常用方法
        public void println(Xxx xx)    打印任意类型的数据出去
        public void write(int/byte[]/byte[]一部分)    可以支持写字节数据出去(PrintStream)
        public void write(int/String/char[]/..)    可以支持写字符数据出去(PrintWriter)

  • PrintStream和PrintWriter的区别
        打印数据的功能上是一样的:都是使用方便,性能高效
        PrintStream继承自字节输出流OutputStream,因此支持写字节数据的方法
        PrintWriter继承自字符输出流Writer,因此支持写字符数据出去

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //PrintStream(字节打印流)
//        PrintStream printStream = new PrintStream("day09-code/d-1.txt");
//        printStream.println("学IT到白马");
//        printStream.println(1234);
//        printStream.println(true);
//        //共性从父类继承
//        printStream.write(97);//字节或者字节数组
//        printStream.close();;

        //PrintWriter(字符打印流)
        PrintWriter printWriter = new PrintWriter("day09-code/d-2.txt");
        printWriter.println("学IT到白马");
        printWriter.println(1234);
        printWriter.println(true);
        //共性从父类继承
        printWriter.write("hello");
        printWriter.close();;
    }
}
 (5)数据流
  • 数据流
        为了保存数据而用的一种数据流, 数据流输出的数据不是给人看的,是为了保存
        数据输出流输出的数据,只能通过数据输入流读回程序

  • DataOutputStream(数据输出流)
        public DataOutputStream(OutputStream out)  创建新数据输出流包装基础的字节输出流
    
        public final void writeByte(int v) throws IOException  将byte类型的数据写入基础的字节输出流
        public final void writeInt(int v) throws IOException   将int类型的数据写入基础的字节输出流
        public final void writeDouble(Double v) throws IOException 将double类型的数据写入基础的字节输出流
        public final void writeUTF(String str) throws IOException  将字符串数据以UTF-8编码成字节写入基础的字节输出流
        public void  write(int/byte[]/byte[]一部分)   支持写字节数据出去
    
  • DataInputStream(数据输入流)
        public DataInputStream(InputStream is) 创建新数据输入流包装基础的字节输入流
    
        Public final byte readByte() throws IOException    读取字节数据返回
        public final int readInt() throws IOException  读取int类型的数据返回
        public final double readDouble() throws IOException    读取double类型的数据返回
        public final String readUTF() throws IOException   读取字符串数(UTF-8)据返回
        public int readInt()/read(byte[])  支持读字节数据进来
    
    
public class Demo1 {
    public static void main(String[] args) throws Exception {
        //创建数据输出流,写出数据到文件
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("day09-code/e-1.txt"));
        //io操作
        dos.writeByte(100);
        dos.writeInt(100);
        dos.writeDouble(99.9);
        dos.writeUTF("中文");
        //关闭
        dos.close();

        //创建数据输入流,读取数据到程序
        DataInputStream dis = new DataInputStream(new FileInputStream("day09-code/e-1.txt"));
        //io操作
        byte readByte = dis.readByte();
        System.out.println((char) readByte);

        int readInt = dis.readInt();
        System.out.println(readInt);

        double readDouble = dis.readDouble();
        System.out.println(readDouble);

        String readUTF = dis.readUTF();
        System.out.println(readUTF);
        //关闭
        dis.close();
    }
}
 (6)序列化流
  • 序列化与反序列化
        序列化: 将内存中的对象保存到磁盘文件
        反序列化: 将磁盘文件中的数据还原成内存对象

  • ObjectOutputStream(OutputStream out) 可以把Java对象进行序列化:把Java对象存入到文件中去。
        public final void writeObject(Object obj) 把对象写出去
    
    public ObjectInputStream(InputStream is) 创建对象字节输入流,包装基础的字节输入流
        public final Object readObject()   把存储在文件中的Java对象读出来
    
  •  注意

        对象如果要参与序列化,必须实现序列化接口(java.io.Serializable)

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //0. 准备一个Student对象
        Student student = new Student("张三", 18);

        //将学生对象存入文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day09-code/f-1.txt"));
        //将数据存入文件
        oos.writeObject(student);
        //关闭流
        oos.close();

        //从文件中读取学生对象
        //创建对象字节输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day09-code/f-1.txt"));
        //读取对象
        Student stu = (Student) ois.readObject();
        System.out.println(stu);
        //关闭流
        ois.close();

    }
}

//学生类
class Student implements Serializable {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    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 +
                '}';
    }
}
  • 对象存储
        用一个ArrayList集合存储多个对象,然后直接对集合进行序列化即可
        注意:ArrayList集合已经实现了序列化接口!

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //0. 准备一个Student对象的集合
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 18));
        students.add(new Student("李四", 19));
        students.add(new Student("王五", 20));

        //1. 序列化(f-1.txt)
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/3.txt"));
        //写入文件
        oos.writeObject(students);
        //关闭
        oos.close();
        //2. 反序列化(f-1.txt)
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/3.txt"));
        //读取文件内容,还原成对象
        List<Student> obj = (List<Student>) ois.readObject();
        System.out.println(obj);
        //关闭
        ois.close();
    }
}
(7)资源释放
try-catch-finally
    try {
        ...
    } catch (IOException e) {
        ...
    }finally{
        ...
    }
注意:
    1. finally代码块写在try-catch的最后面
    2. 无论try的代码是否出现问题, 最后一定会执行finally中的代码,除非JVM终止

public class Demo1 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            //1. 创建文件字节输入流、输出流
            fis = new FileInputStream("D:/upload/other/duotai1.wmv");
            fos = new FileOutputStream("D:/upload/other/duotai2.wmv");

            //2. 使用输入流读取内容,使用输出流写出内容
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //程序不论正常执行或异常执行,最终都会执行的代码块
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
try-with-resource
    JDK7开始提供的释放资源方式,编码更简洁


书写格式
    try(资源对象1,资源对象2..){
        ...
    }catch(Exception e){
        处理异常的代码
    }

注意:
    try后面小括号中只能定义资源对象,最终底层会帮我们释放这些对象
public class Demo2 {
    public static void main(String[] args)  {
        try(
            //1. 创建文件字节输入流、输出流
            FileInputStream fis = new FileInputStream("D:/upload/other/duotai1.wmv");
            FileOutputStream fos = new FileOutputStream("D:/upload/other/duotai2.wmv");
        ) {
            //2. 使用输入流读取内容,使用输出流写出内容
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

finally相关面试题
  1. try-catch-finally 中哪个部分可以省略?
  2. final,finally和finalize的区别
  3. 关于finally在try-catch中的执行顺序
    当try和catch代码块中有return语句时,finally仍然会被执行
    执行try或catch代码块中的return语句之前,都会先执行finally语句
    finally代码块中的return语句一定会执行
    无论在finally代码块中是否有返回值,返回值都不会改变,仍然是执行finally代码块之前的值

public class Demo3 {
    public static void main(String[] args) {
        //System.out.println(m1());
        //System.out.println(m2());
        //System.out.println(m3());
        System.out.println(m4());
    }

    //当try和catch代码块中有return语句时,finally仍然会被执行
    //执行try或catch代码块中的return语句之前,都会先执行finally语句
    public static int m1() {
        try {
            return 1;
        } finally {
            System.out.println("执行finally模块");
        }
    }

    public static int m2() {
        try {
            int a = 8 / 0;
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            System.out.println("执行finally模块");
        }
    }

    //finally代码块中的return语句一定会执行
    public static int m3() {
        try {
            int a = 8 / 0;
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            System.out.println("执行finally模块");
            return 0;
        }
    }

    //无论在finally代码块中是否有返回值,返回值都不会改变,仍然是执行finally代码块之前的值
    public static int m4() {
        int result = 0;
        try {
            return result;
        } finally {
            System.out.println("执行finally模块");
            result = 1;
        }
    }


}
(8)IO框架
  • 框架的形式
        一般是将类、接口等编译成class文件形式,再压缩成一个jar包发行(供别人使用)

  • Commons-io
        是Apache开源基金组织提供的一组关于IO操作的小框架,目的是提高IO相关操作开发的效率

  • 使用步骤
        1、在模块下新建Directory文件夹,名字叫libs
        2、将准备好的jar包复制到lib文件夹
        3、对着libs目录右键Add as Library..添加到项日环境中
        4、修改level改为module Library

  • 常用工具类FileUtils
        public static void copyFile(File数据源,File目的地) 复制文件
        public static void copyDirectory(File 数据源,File目的地) 复制文件夹
        public static void deleteDirectory(File 目标文件夹) 删除文件夹(有内容也直接删)
        public static String readFileToString(File 数据源,String encoding) 读数据
        public static void writeStringToFile(File file,String data,String encoding, Boolean append) 写数据

    常用工具类IOUtils类
        public static int copy(InputStream inputStream, OutputStream outputStream) 复制文件
        public static int copy(Reader reader, Writer writer)   复制文件
        public static void write(String data, OutputStream output, String charsetName)     写数据

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //FileUtils:文件操作的工具类
        //File file1 = new File("D:\\upload\\other\\duotai.wmv");
        //File file2 = new File("D:\\upload\\other\\duotai2.wmv");
        //FileUtils.copyFile(file1,file2); //复制文件

        //复制文件夹
        //FileUtils.copyDirectory(new File("D:\\upload"),new File("D:\\upload1"));
        //删除文件夹(有内容也直接删)
        //FileUtils.deleteDirectory(new File("D:\\upload1"));
        // 读数据
        //String string = FileUtils.readFileToString(new File("D:\\test.txt"), "GBK");
        //System.out.println(string);

        //向文件写入内容
        //String str = "好好学习,good good study \r\n";
        //FileUtils.writeStringToFile(new File("D:\\test1.txt"),str,"UTF-8",true);//文件,数据,编码,是否追加

        //IOUtils类 : 简化IO流操作
        //复制文件:使用字节流复制(所有文件)
        FileInputStream fis = new FileInputStream("D:\\upload\\other\\duotai.wmv");
        FileOutputStream fos = new FileOutputStream("D:\\upload\\other\\duotai3.wmv");
        IOUtils.copy(fis,fos);
        //复制文件:使用字符流复制(所有文本文件)
        //FileReader reader = new FileReader("D:\\test1.txt");
        //FileWriter writer = new FileWriter("D:\\test2.txt");
        //IOUtils.copy(reader,writer);
        //writer.close();
        //写数据
        //IOUtils.write("学it到黑马",new FileOutputStream("D:\\test3.txt"),"UTF-8");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值