Java,FileInputStream、FileOutputStream字节流一般性操作

这篇博客展示了多个Java文件操作的实例,包括使用字节流进行文件拷贝、读取、写入、缓冲处理以及加密操作。通过不同方法实现文件的复制,如逐个字节、大数组、小数组、Buffered流等,同时包含异常处理的代码示例。
摘要由CSDN通过智能技术生成

在这里插入图片描述

package xyz.zlqhe.www;

import java.io.*;
import java.util.Scanner;

public class MainTest {
    public static void main(String[] args) throws IOException {
        //拷贝:
        //Demo_01(); //逐个字节
        //Demo_02(); //大数组
        //Demo_03(); //小数组
        //Demo_04(); //Buffered
        //Demo_05();  //拷贝读取中文,标准异常处理代码
        //Demo_06(); //1.7版本之后标准异常处理
        //Demo_07();  //文件加密
        Demo_08();   //记事本模板
    }

    public static void Demo_08() throws IOException{
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入记事本名字:");
        String s = sc.nextLine() + ".txt";
        System.out.println("此为记事本,请输入:");
        FileOutputStream fileOutputStream = new FileOutputStream(s);
        while(true){
            String s2 = sc.nextLine();
            if(s2.equals("QUIT")) break;
            fileOutputStream.write(s2.getBytes());
            fileOutputStream.write("\r\n".getBytes());
        }
    }

    private static void Demo_07() throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("AfterBuf.mp3"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("AfterBuf2.mp3"));
        int b;
        while((b = bis.read()) != -1) {
            bos.write(b ^ 123);
        }
        bis.close();
        bos.close();
    }

    private static void Demo_06() throws IOException{
        FileOutputStream fout = new FileOutputStream("bbb.txt");
        String s = "你好世界";
        fout.write(s.getBytes());
        fout.close();
        //标准异常处理代码
        try(FileInputStream fis = new FileInputStream("bbb.txt");
            FileOutputStream fos = new FileOutputStream("copy_bbb.txt");){
            byte[] arr = new byte[4];
            int length;
            while ((length = fis.read(arr)) != -1) {
                fos.write(arr, 0, length);
            }
        }
    }

    private static void Demo_05() throws IOException{
        FileOutputStream fout = new FileOutputStream("bbb.txt");
        String s = "你好世界";
        fout.write(s.getBytes());
        fout.close();
        //标准异常处理代码
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("bbb.txt");
            fos = new FileOutputStream("copy_bbb.txt");
            byte[] arr = new byte[4];
            int length;
            while ((length = fis.read(arr)) != -1) {
                fos.write(arr, 0, length);
            }
        }finally {
            try{
                if(fis != null) fis.close();
            }finally {
                if(fos != null) fos.close();
            }
        }
    }

    private static void Demo_04() throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("一腔诗意喂了狗.mp3"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("0"));
        int b;
        while ((b=bis.read())!=-1) {
            bos.write(b);
        }
        bis.close();
        bos.close();
    }

    private static void Demo_03() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("一腔诗意喂了狗.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream("O.mp3");
        byte[] arr = new byte[1024 * 8];
        int length;
        while((length = fileInputStream.read(arr)) != -1) {
            fileOutputStream.write(arr,0,length);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    private static void Demo_02() throws IOException{
        FileInputStream fileInputStream = new FileInputStream("一腔诗意喂了狗.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream("o.mp3");
        byte[] fis = new byte[fileInputStream.available()];
        fileInputStream.read(fis);
        fileOutputStream.write(fis);
        fileInputStream.close();
        fileOutputStream.close();
    }

    private static void Demo_01() throws IOException{
        FileInputStream fileInputStream = new FileInputStream("a.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("b.jpg");
        int a;
        while((a = fileInputStream.read()) != -1){
            fileOutputStream.write(a);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dylan、

耕码不易,白嫖可耻

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值