io输出流API



import java.io.FileOutputStream;
import java.io.OutputStream;

public class OutputStreamDemo1 {
    public static void main(String[] args) throws Exception {

        //创建管道流通方式;
        OutputStream tt = new FileOutputStream("C:\\Users\\YY\\Desktop\\vv.txt");

        //2.从内存写数据出去(写一个字节出去);
        tt.write('a');
        tt.write(98);
        //tt.write('吴');默认写一个字节,会造成乱码。

        //3.写一个是数组出去
        byte[] arr = {'a', 101, 98, 100};
        tt.write(arr);
        tt.write("\r\n".getBytes());
        byte[] arr1 = "我来自中国安徽合肥".getBytes();
        tt.write(arr1);
        //4.写字节的一部分
        byte[] arr2={97,98,99,100};
        tt.write(arr2,1,5);

        tt.close();//一定要关闭管道,防止占用资源,消耗CPU(同时生效刷新管道)
    }
}

2.拷贝文件



import java.io.*;

public class Copy {
    public static void main(String[] args) {
        //目标:拷贝视频文件;
        try {

            //声明一个输入流管道与原视频接通;
            InputStream bb = new FileInputStream("C:\\Users\\YY\\Desktop\\JAVA张老师\\汽车租赁系统\\【04】需求讲解-2.avi");
            //声明一个输出流管道与目标文件接通;
            OutputStream cc = new FileOutputStream("C:\\Users\\YY\\Desktop\\汽车car.avi");
           /* byte[] buffer = new byte[1024];
            int b;
            while ((b = bb.read(buffer)) != -1) {
                cc.write(buffer, 0, b);
            }*/
            byte[] nn= bb.readAllBytes();
            cc.write(nn);
            System.out.println("复制完成!");
            bb.close();
            cc.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}

3.资源释放的三种方法;


基本做法:
import java.io.*;

public class FreeResources {
    public static void main(String[] args) {

        InputStream bb = null;
        OutputStream cc = null;
        try {
            //01管道做出之前出现异常
            // System.out.println(10/0);
            bb = new FileInputStream("C:\\Users\\YY\\Desktop\\JAVA张老师\\汽车租赁系统\\【04】需求讲解-2.avi");
            cc = new FileOutputStream("C:\\Users\\YY\\Desktop\\汽车car.avi");
            byte[] nn = bb.readAllBytes();
            cc.write(nn);
            System.out.println("复制完成!");
            //2情况:中途遇到异常,资源未得到关闭
            System.out.println(10 / 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //2
            try {
                //1
                if (bb != null)
                    bb.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (cc != null)
                    cc.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
JDK9;


import java.io.*;

public class FreeResources3 {
    public static void main(String[] args) throws FileNotFoundException {
        InputStream bb = new FileInputStream("C:\\Users\\YY\\Desktop\\JAVA张老师\\汽车租赁系统\\【04】需求讲解-2.avi");
        OutputStream cc = new FileOutputStream("C:\\Users\\YY\\Desktop\\汽车car.avi");
        try  {
            byte[] nn = bb.readAllBytes();
            cc.write(nn);
            System.out.println("复制完成!");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
//不建议使用

JDK7:


import java.io.*;

public class FreeResources2 {
    public static void main(String[] args) {
        try (InputStream bb = new FileInputStream("C:\\Users\\YY\\Desktop\\JAVA张老师\\汽车租赁系统\\【04】需求讲解-2.avi");
             OutputStream cc = new FileOutputStream("C:\\Users\\YY\\Desktop\\汽车car.avi");) {
            byte[] nn = bb.readAllBytes();
            cc.write(nn);
            System.out.println("复制完成!");
            System.out.println(10 / 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
//try(放资源,自动实现close,因为它实现closeable接口)

文件字符输入出流:

//文件字符输入流的三种方式;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

public class FileReaderDemo {
    public static void main(String[] args) throws Exception {
        //字符输入;

        //创建字符输入流管道
        Reader cc = new FileReader("F:\\IDEA\\Pepper\\myself\\src\\cyy701\\bb");
        //2.如果没有可读则返回-1
        //1.每次读取一个字符
      /*  int code =cc.read();
        System.out.println((char) code);*/

        //3循环读
       /* int code;
        while ((code=cc.read())!=-1){
            System.out.print((char) code);
        }
*/
        //数组读字符流
        /*char[] buffer = new char[1024];
        int code;
        while ((code = cc.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, code));
        }*/
        
    }
}


//文件字符输出流


import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class FileWriteDemo {
    public static void main(String[] args) throws Exception {
        //不需要创建文件。自动生成文件;
        Writer ss = new FileWriter("C:\\Users\\YY\\Desktop\\新建文本文档1.txt");
        //一个一个写(注意:最后一定要close!!!!!!!!)
        ss.write(98);
        ss.write('曹');
        ss.write('洋');
        ss.write('洋');
        ss.write("我爱中国");
        ss.write('\r');


        //2.通过数组写(其实可以直接写字符串了)
        char[] buffer = "我是一个中国人".toCharArray();
        ss.write(buffer);
        ss.write('\r');

        ss.write("abc我是一名小学生!", 0, 2);
        ss.flush();//刷新
        ss.write("abc我是一名小学生!", 2, 6);
        ss.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值