输入输出流

一:字节流

public void test() throws Exception {
        File file = new File("D:\\zsc\\ccc.mp4");
        FileInputStream fis=new FileInputStream(file);
        FileOutputStream fos=new FileOutputStream(file);
        byte by[] = new byte[1024];
        long count = file.length()%1024 == 0 ? file.length()/1024 : file.length()/1024 + 1;
        for(int i=0;i<count;i++){
            fis.read(by);
            fos.write(by);
            fos.flush();
            fos.close();
            fis.close();
        }

    }

二:字符流

public void test() throws Exception {
        File file1 = new File("D:\\zsc\\ccc.mp4");
        String file2="D:\\zsc"+File.separator+"ccc.mp4";
        String str="wogwpgwpgfgowg";
        //输入流
        FileReader fr=new FileReader(file1);
        BufferedReader bfr=new BufferedReader(fr);
        //输出流
       FileWriter fw=new FileWriter(file2,true);  //不覆盖原有数据
        BufferedWriter bfw=new BufferedWriter(fw);

        bfr.readLine();
        bfr.read();
        char ch[] = new char[1024];
        long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
        for(int i=0;i<count;i++){
            bfr.read(ch);
        }
        bfw.write(str);
        bfw.flush();
    }

三:使用字节流读取二进制文件

public void test() throws Exception {
        File file1 = new File("D:\\zsc\\ccc.mp4");
        String file2="D:\\zsc"+File.separator+"ccc.mp4";
        String str="wogwpgwpgfgowg";
        //输入流
        FileInputStream fis=new FileInputStream(file1);
        DataInputStream dis=new DataInputStream(fis);
        //输出流
        FileOutputStream fos=new FileOutputStream(file2);
        DataOutputStream dos=new DataOutputStream(fos);

        dis.read();
        //读取出来的是整数
        dis.readInt();
        byte by[] = new byte[1024];
        long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
        for(int i=0;i<count;i++){
            dis.read(by);
        }
        //把数据写入二进制文件
        dos.write(1);
        dos.write(by);
        dos.flush();
    }

四:设置流的输入输出格式

public void test() throws Exception {
        File file1 = new File("D:\\zsc\\ccc.mp4");
        String file2="D:\\zsc"+File.separator+"ccc.mp4";
        String str="wogwpgwpgfgowg";
        //输入流
        FileInputStream fis=new FileInputStream(file1);
        //设置输入格式
        InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
        
        //输出流
        FileOutputStream fos=new FileOutputStream(file2,true);  //不覆盖原有数据
        //设置输出格式
        OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
        
        isr.read();
        char ch[] = new char[1024];
        long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
        for(int i=0;i<count;i++){
            isr.read(ch);
        }
        osw.write(str);
        osw.flush();
    }

五:键盘输入流

public void test() throws Exception {
        File file1 = new File("D:\\zsc\\ccc.mp4");
        String file2="D:\\zsc"+File.separator+"ccc.mp4";
        String str="wogwpgwpgfgowg";
        //键盘输入
        InputStreamReader isr=new InputStreamReader(System.in,"UTF-8");
        BufferedReader bfr=new BufferedReader(isr);
        //输出
        FileWriter fw=new FileWriter(file1);
        BufferedWriter bw=new BufferedWriter(fw);
        
        bfr.readLine();
        bfr.read();
        bw.write(str);
        bw.flush();
    }

六:序列化和反序列化
注意:
1,只有实现了Serializable接口的类的对象才能实序列化
2,如果一个可序列化的类有多个父类,则这些父类要么也是可序列化的,要么有无参的构造器

 public void test() throws Exception {
        File file1 = new File("D:\\zsc\\ccc.mp4");
        String file2="D:\\zsc"+File.separator+"ccc.mp4";
        Bbs_User user=new Bbs_User();
        //序列化,输出流
        FileOutputStream fos=new FileOutputStream(file2);
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        
        //反序列化,输入流
        FileInputStream fis=new FileInputStream(file1);
        ObjectInputStream ois=new ObjectInputStream(fis);
        
        oos.writeObject(user);
        oos.flush();
        ois.readObject();
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值