Java IO字节输出流的学习记录

2021/9/6 20:08:39

本文主要是介绍Java IO字节输出流的学习记录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.kj.test;

import cn.hutool.core.io.IoUtil;

import java.io.*;

/**
 * Java IO字节输出流的总结
 */
public class FileOutputTest {

    public static void main(String[] args) {
        /**
         * 文件写入流的使用 FileOutputStream
         */
        // 写文件的地方
        File file = new File("D:/output.txt");
        // 文件写入流
        FileOutputStream fos = null;
        try {
            /**
             * 不用判断文件是否存在,系统将会把你的输入名当成文件
             *
             * 使用字节流向目标文件写入内容
             * 如果第二个参数为true,表示在原来内容上追加内容
             */
            fos = new FileOutputStream(file, true);
            fos.write("test IO input \r\n 123".getBytes());
            fos.write("test IO input \r\n 123".getBytes());

            //  一般要把缓存刷入
            fos.flush();

        } catch (IOException e) {
            e.getMessage();
        } finally {
            IoUtil.close(fos);
        }


        /**
         * 高效的字节输出流
         *  BufferedOutputStream的使用
         */
        // 定义一个输出流
        FileOutputStream fileOutputStream = null;

        // 定义一个缓冲输出流
        BufferedOutputStream bos = null;

        try {
            //把输出的地址给输出流
            fileOutputStream = new FileOutputStream("D:/bufout.txt");
            // 把输出流对象传递给缓冲输出流
            bos = new BufferedOutputStream(fileOutputStream);

            // 往缓冲流里面写入文件
            bos.write("bufout".getBytes());

            // 刷新缓冲
            bos.flush();
        } catch (IOException e) {
            e.getMessage();
        } finally {
            IoUtil.close(fileOutputStream);
            IoUtil.close(bos);
        }

        /**
         * PrintWriter 的使用:这是最方便简单的写文件方式,主要是对文本文件有用!
         */

        // 打印流
        PrintWriter pw = null;
        try {
            // 写入文件的路径
            pw = new PrintWriter("D:/printWriter.txt");
            pw.write("123\r\n");
            pw.write("123\r\n");
            pw.write("123\r\n");
            pw.write("123\r\n");

            // 刷新下缓冲
            pw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(pw);
        }

        // 以上是文件写入的3中基本方式,前面两种是基本的写入方式,
        //可以对任何格式的文件再读取后进行写入,第三种主要是快速实现文字的写入。


        // 前面的例子都是对字符串的写入,下面对其他文件进行写入。
        //这里还设计到读取文件,实际操作是边读边写。
        /**
         * copy一张图片,从一个目录复制到另一个目录下
         *
         * 程序设计思路:
         * 1、在在目的地创建一个图片文件
         * 2、读取源地址的字节流写入到目的地地址的文件里面
         * 3、刷新输出流,并关闭流
         */
        // 把D盘下的mm.jpg复制到D盘java目录下
        // 源文件
        File imgFileFrom = new File("D:/mm.jpg");

        // 目的地文件目录
        File dir = new File("D:/java");
        // 先创建目录,确保目录存在
        dir.mkdirs();
        File imgFileTo = new File("D:/java/mm.jpg");
        FileInputStream fis = null;
        FileOutputStream fots = null;
        try {
            // 创建目的文件地址,如果已存在则会创建失败
            if (!imgFileTo.createNewFile()) {
                System.out.println("创建文件失败");
            }
            // 读取源地址文件的字节流
            fis = new FileInputStream(imgFileFrom);
            fots = new FileOutputStream(imgFileTo);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = fis.read(buf)) != -1) {
                // 把读取的字节流写入到目的文件中
                fots.write(buf, 0, len);
            }
            // 刷新下输出流
            fots.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(fis);
            IoUtil.close(fots);
            System.out.println("文件复制成功");
        }
    }

}


这篇关于Java IO字节输出流的学习记录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程