字节流和字符流简单操作

常用的I/O流:
在这里插入图片描述
1.字节流:

/**
     * 输出字节流--写操作
     *
     * @param filePath
     */
    public static void fileOutputStreamTest(String filePath) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            String str = "测试FileOutputStream";
            fos.write(str.getBytes(StandardCharsets.UTF_8));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 输入字节流--读操作
     *
     * @param filePath
     */
    public static void fileInputStreamTest(String filePath) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            byte[] bytes = new byte[1024];
            int len = 0;
            StringBuffer buffer = new StringBuffer();
            while ((len = fis.read(bytes)) != -1) {
                buffer.append(new String(bytes));
            }
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 使用字节流--复制文件
     *
     * @param srcFile
     * @param destFile
     */
    public static void fileCopy(String srcFile, String destFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  /**
     * 缓冲字节输出流--读操作
     *
     * @param filePath
     */
    public static void bufferedInputStreamTest(String filePath) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(filePath);
            bis = new BufferedInputStream(fis);
            byte[] bytes = new byte[1024];
            int len = 0;
            StringBuffer buffer = new StringBuffer();
            while ((len = bis.read(bytes)) != -1) {
                buffer.append(new String(bytes));
            }
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 缓冲字节输出流--写操作
     *
     * @param filePath
     */
    public static void bufferedOutputStreamTest(String filePath) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(filePath);
            bos = new BufferedOutputStream(fos);
            String str = "测试BufferedOutputStream";
            bos.write(str.getBytes(StandardCharsets.UTF_8));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2.字符流:

 /**
     * 字符输入流--读操作
     *
     * @param filePath
     */
    public static void fileReaderTest(String filePath) {
        FileReader fr = null;
        try {
            fr = new FileReader(filePath);
            char[] chars = new char[1024];
            int len = 0;
            StringBuffer buffer = new StringBuffer();
            while ((len = fr.read(chars)) != -1) {
                buffer.append(new String(chars));
            }
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符输出流--写操作
     *
     * @param filePath
     */
    public static void fileWriterTest(String filePath) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(filePath);
            String str = "测试FileWriter";
            fw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 /**
     * 缓冲字符输入流--读操作
     *
     * @param filePath
     */
    public static void bufferedReaderTest(String filePath) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(filePath);
            br = new BufferedReader(fr);
            StringBuffer buffer = new StringBuffer();
            String res = null;
            while ((res = br.readLine()) != null) {
                buffer.append(res);
            }
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 缓存字符输出流--写操作
     *
     * @param filePath
     */
    public static void bufferedWriterTest(String filePath) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(filePath);
            bw = new BufferedWriter(fw);
            String str = "测试FileWriter写入内容";
            bw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.字节流转换为字符流:

 /**
     * 字节输入流--转换--字符输出流 ---读操作
     *
     * @param filePath
     */
    public static void inputStreamReaderTest(String filePath) {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(filePath);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            String res = null;
            StringBuffer buffer = new StringBuffer();
            while ((res = br.readLine()) != null) {
                buffer.append(res);
            }
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                isr.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节输出流--转换--字符输出流--写操作
     *
     * @param filePath
     */
    public static void outputStreamWriterTest(String filePath) {
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            fos = new FileOutputStream(filePath);
            osw = new OutputStreamWriter(fos);
            bw = new BufferedWriter(osw);
            String str = "测试OutputStreamWriter";
            bw.write(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                osw.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

注意事项:
字节流:处理音频文件、图片、歌曲比较好,不会导致文件损坏,如使用字符流处理则会出现文件损坏。
字符流:处理word文档,文本类文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值