Java——IO流

IO流

按流向分输入流输出流,按数据类型分为字节流(能操作所有类型的文件)和字符流(用记事本能正常打开不乱码的就是字符文件)。
Input,输入,读,也就是数据来的时候用。数据从哪里来?内存、文件、网络。
Output,输出,写,也就是数据去的时候用。数据到哪里去?内存、文件、网络。

IO流的两个方法

  1. flush() 刷新流,还可以继续写数据
  2. close() 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据。且流用完之后必须关闭。

字节流

可以操作所有类型的文件。不止字节文件。

字节流写数据

字节流写数据的步骤

  1. 创建字节输出流对象。如果文件不存在就创建;如果文件存在就清空
  2. 写数据。写出的整数,实际上写出的诗整数在码表上对应的字幕
  3. 释放资源。每次使用完流必须要释放资源

字节流写数据的三种方式

  1. write(int b) 一次写一个字节数据
  2. write(byte[] b) 一次写一个字节数组数据
  3. write(byte[] b,int off,int len)一次写一个字节数组的部分数据,可以固定在什么地方去取,断点续传就是这个原理

字节流写数据如何做到换行和追加内容

换行:

  • 使用转义字符\r\n即可实现换行操作。
    追加内容:
  • public FileOutputStream(String name, boolean append),创建文件输出流以指定的名称写入文件,如果第二个参数为true,则不会清空文件里面的内容。

例子

public static void main(String[] args) {
        /**
         * 写一段文字到文件里面
         * 1.有文字
         * 2.要有文件
         * 3.要有目录
         * 4.创建目录
         * 2.要有目录路径和文件名
         */
        //得到数据
        IOWrite myWrite = new IOWrite();
        String str = "真是一段没有实际意义的文字";
        String path = "C:\\Users\\Administrator\\Desktop\\IO流的文件夹";
        String fileName = "一个文本文档.txt";

        //创建目录
//        File directory = new File(path);
        File directory = myWrite.crearteDirectory(path, "");
        //创建文件
//        File file = new File(directory, fileName);
        File file = myWrite.createFile(directory, fileName);
        //写数据  将内存里的数据写入到硬盘
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
//            fileOutputStream.write(97);//实际写入的是a,因为a的ASCII码就是97
            //怎么将String转换为int或者byte数组
            //不能直接将String转换为int,因为直接转换会默认转换为ASCII码
            //所以只能将字符串转换为byte数组
//            fileOutputStream.write(Integer.parseInt(str));
            fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();//用完必须关闭
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回创建的目录
     *
     * @param parent
     * @param child
     * @return
     */
    public File crearteDirectory(String parent, String child) {
        File file = new File(parent, child);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 返回创建的问文件
     *
     * @param directory
     * @param fileName
     * @return
     */
    public File createFile(File directory, String fileName) {
        File file = new File(directory, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

在这里插入图片描述

字节流读数据

字节流读数据的步骤

  1. 创建字节输入流对象。如果文件不存在,就直接报错
  2. 读数据。读出来的是文件中数据的码表值,比如a-》97
  3. 释放资源。每次使用完必须要释放资源

字节流读数据的方式

int read​() 一次读一个字符数据
int read​(char[] cbuf) 一次读一个字符数组数据

例子

public static void main(String[] args) {
        /**
         * 需要目录和文件路径
         * 读取文件
         */
        IORead ioRead = new IORead();

        String path = "C:\\Users\\Administrator\\Desktop\\IO流的文件夹";
        String fileName = "IO读.txt";

        File file = new File(path, fileName);

        String read = ioRead.read(file);
        System.out.println(read);
    }

    public String read(File file){
        StringBuilder a = new StringBuilder();
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            int read = 0;
            //直接读取出来不认识,一般称为乱码
            //为什么会出现乱码?因为FileInputStream叫字节流,每次只能读一个字节
            //但是中文一个汉字三个字节,所以应该用字符流去读
            //读取的时候有很多个字符,需要将所有的都读取出来
            while ((read = fileInputStream.read()) != -1){
                char cc = (char) read;
                a.append(cc);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a.toString();
    }

拷贝文件

原理就是先读文件,读出来之后将读取到的内容再写入另一个文件,可以读完后再写,但是推荐边读边写。

public static void main(String[] args) {
        //一次性,写入多个数据
        IOWrite myWrite = new IOWrite();
        String path = "C:\\Users\\Administrator\\Desktop\\IO流的文件夹";
        String fileName = "一个文本文档的副本.txt";

        File directory = myWrite.crearteDirectory(path, "");
        File file = myWrite.createFile(directory, fileName);
        //1.7版本之后的写法,try-with-resources,自动帮我们关闭
        try(FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\一个文本文档.txt")) {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
//            int content = 0;//每次只读一个
            byte[] content = new byte[1024 * 1024];//每一次批量读取
            int length = 0;
            while ((length = fileInputStream.read(content)) != -1){
                fileOutputStream.write(content);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public File crearteDirectory(String parent, String child) {
        File file = new File(parent, child);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 返回创建的问文件
     *
     * @param directory
     * @param fileName
     * @return
     */
    public File createFile(File directory, String fileName) {
        File file = new File(directory, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

拷贝并加密

有两种方法,本质上都是让拷贝之后不能随意打开。
最简单的一种方法是拷贝文件写入新文件的时候,不加上文件类型,需要打开的时候手动加上文件类型。
另一种是在拷贝的时候在文件里面加入一段其他的内容,需要解密的话只需要读的时候将多出来的那段内容删除。不过对txt这种没有作用。

public static void main(String[] args) {
        CopyFile copyFile = new CopyFile();

        String oldPath = "C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\test.jpg";
        //最简单的加密,存的时候不加上文件类型,需要的时候手动加上
        // 或者在写入文件的时候多写一行,解密的时候就在读取文件将那一行删除
        String newPath = "C:\\Users\\Administrator\\Desktop\\IO流副本文件夹\\test";//没有文件类型,要打开需要手动加上文件类型
        String parentPath = copyFile.getPath(newPath);
        String fileName = copyFile.getFileName(newPath);

        File directory = Tool.crearteDirectory(parentPath, "");
        File file = Tool.createFile(directory, fileName);

        copyFile.copy(oldPath, newPath);
    }

    /**
     * 获取目录路径
     * @param path
     * @return
     */
    public String getPath(String path){
        String[] arr = path.split("\\\\");//有转义字符,前两个是转义字符,后两个才是需要的
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            stringBuilder.append(arr[i]);
        }
        return stringBuilder.toString();
    }

    /**
     * 获取文件名
     * @param path
     * @return
     */
    public String getFileName(String path){
        String[] arr = path.split("\\\\");//有转义字符,前两个是转义字符,后两个才是需要的
        return arr[arr.length - 1];
    }

    public void copy(String oldPath, String newPath){
        try {
            FileInputStream fileInputStream = new FileInputStream(oldPath);
            //TODO:有问题,找不到路径
            FileOutputStream fileOutputStream = new FileOutputStream(newPath);
            int read = 0;
            while ((read = fileInputStream.read()) != -1){
                fileOutputStream.write(read);
            }
            fileInputStream.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

字节缓冲流

BufferedInputStream与BufferedOutputStream 可以为InputStream、OutputStream 增加缓冲区功能。
BufferedInputStream的默认缓冲区大小是8192,当读取数据时BufferedInputStream会把这个缓冲区填满,当使用read() 方法时,实际上是从缓冲区读取数据,而不是直接读取源数据。当缓冲区数据不足时,BufferedInputStream 才会操作给定的 InputStream 的read() 方法读取数据到缓冲区。
缓冲区的作用主要是提高文件的读写功能,减少程序频繁从文件中获取数据和写入数据。

public static void main(String[] args) {
        String sourcePath = "C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\IO读.txt";
        try(FileInputStream fileInputStream = new FileInputStream(sourcePath);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\字节缓冲流.txt");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        ) {
            long start = System.currentTimeMillis();
            byte[] content = new byte[1];
            System.out.println("开始读取文件");
            while (bufferedInputStream.read(content) != -1){
                bufferedOutputStream.write(content);
            }
            long end = System.currentTimeMillis();
            System.out.println("文件写入结束,耗时:" + (end - start) + "ms");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用缓冲,最终耗时60毫秒。

字符流

只能操作字符文件,能用记事本正常打开不乱码的就是字符文件。
把文件中的数据读取到内存时,如果此时文件中出现了中文,那么字节流就会出现乱码现象。所以纯文本的文件,我们就需要使用字符流来进行操作。

字符编码

那么为什么会有乱码呢?这有可能牵涉到编码表。

编码表

Java中的字符使用的都是Unicode字符集,编码方式为UTF-16。
计算机中储存的信息都是用二进制数表示的;我们在屏幕上看到的英文、汉字等字符是二进制数转换之后的结果。
按照某种规则,将字符存储到计算机中,称为编码。
按照同样的规则,将存储在计算机中的二进制数解析显示出来,称为解码 。
编码和解码的方式必须一致,否则会导致乱码。
GBK,GB2312会将一个汉字拆成2个字节,UTF-8拆成3个字节,UTF-16拆成4个字节。而ascii和iso都只有1个字节,无法正确显示一个中文。
一旦出现乱码就需要考虑字符集的使用出了问题。
以下是常见的三个字符集:
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码):包括了数字,大小写字符和一些常见的标点符号。
GBKwindow系统默认的码表。兼容ASCII码表,也包含了21003个汉字,并支持繁体汉字以及部分日韩文字。
Unicode码表:由国际组织ISO 制定,是统一的万国码,计算机科学领域里的一项业界标准,容纳世界上大多数国家的所有常见文字和符号。但是因为表示的字符太多,所以Unicode码表中的数字不是直接以二进制的形式存储到计算机的,会先通过UTF-7,UTF-7.5,UTF-8,UTF-16,以及 UTF-32的编码方式再存储到计算机,其中最为常见的就是UTF-8。

字符流读数据

如果字符集没有问题,但是依然乱码了,有可能是因为使用字节流读取纯文本文件的途中出了问题。因为字节流一次读一个字节,而不管GBK还是UTF-8一个中文都是多个字节,用字节流每次只能读其中的一部分,所以就会出现乱码问题。
所以这个时候就需要字符流。

字符流读数据的方式

  1. int read​() 一次读一个字符数据
  2. int read​(char[] cbuf) 一次读一个字符数组数据
    可以看出与字节流相似。
public static void main(String[] args) {
        StringBuilder a = new StringBuilder();
        try(FileReader fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\一个文本文档.txt")) {
            int read = 0;
            while ((read = fileReader.read()) != -1){
                char cc = (char) read;
                a.append(cc);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(a);

    }

在这里插入图片描述

字符流写数据

字符流写数据的方式

  1. void write​(int c) 写一个字符
  2. void write​(char[] cbuf) 写入一个字符数组
  3. void write​(char[] cbuf, int off, int len) 写入字符数组的一部分
  4. void write​(String str) 写一个字符串
  5. void write​(String str, int off, int len) 写一个字符串的一部分
public static void main(String[] args) {
        try(FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\字符流写文件.txt")) {
            String str = "富强民主文明和谐";
            fileWriter.write(str);
        } catch (IOException e){
            e.printStackTrace();
        }
    }

在这里插入图片描述

字符流复制文件

public static void main(String[] args) {
        CharacterCopy characterCopy = new CharacterCopy();
        characterCopy.copyTextFile();
    }
    public void copyTextFile() {
        try(FileReader fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\一个文本文档.txt");
            FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\IO流的文件夹\\字符流复制.txt")) {
            int data = 0;
            while((data = fileReader.read()) != -1) {
                fileWriter.write(data);
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }

虽然字节流读纯文本会乱码,但是用来拷贝却不会,比如之前就用字节流拷贝过纯文本的字符文件。也就是说字节流可以拷贝字符文件,但是字符流不可以拷贝字节文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值