文件字节流与字符流

 文件字节流

在Windows下

绝对路径:C://User/test.txt 相对路径:test.txt

文件字节输入流:

InputStream只是⼀个抽象类,要使⽤还需要具体的实现类。关于InputStream的实现类有很多,基 本可以认为不同的输⼊设备都可以对应⼀个InputStream类,我们现在只关⼼从⽂件中读取,所以使 ⽤FileInputStream

当使用完成一个流之后,必须关闭这个流来完成对资源的释放,否则资源会一直被占用:

    public static void main(String[] args) {
        FileInputStream inputStream = null;
        try{
            inputStream=new FileInputStream("test.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

简化写法(语法糖):

    public static void main(String[] args) {
        try (FileInputStream stream=new FileInputStream("test.txt")){
            System.out.println(stream);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

英文字母占一个字节,中文占三个字节

    public static void main(String[] args) {
        try (FileInputStream stream=new FileInputStream("test.txt")){
            //剩余多少个字节
            System.out.println(stream.available());

            //读一个字符
            int x =stream.read();
            System.out.println((char) x);

            //读所有的,前面读取了第一个字母H,此处从后开始读取
            int i;
            while ((i=stream.read())!=-1){
                System.out.print((char) i);
            }
            //所有内容读取完毕,后面的都不输出了
            
            //分开看方法
            byte[] bytes =new byte[3];
            while (stream.read(bytes)!=-1)
                System.out.println(new String(bytes)); //仅限纯文本变成字符串打印出来

            byte[] bytes1=new byte[stream.available()];
            stream.read(bytes1);
            System.out.println(new String(bytes1));
            
            //跳过n个字节读取
            stream.skip(3);
            System.out.println((char) stream.read());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

文件字节输出流:

        //文件字节输出流
        try(FileOutputStream stream1=new FileOutputStream("test1.txt",true)) { //true表示开启追加模式
            stream1.write('c');
            stream1.write("Hello World!".getBytes());
            stream1.write("Hello World!".getBytes(),3,3);//从3开始写3个长度:lo ;
            stream1.flush();//建议在最后执行一次刷新操作(强制写入)来保证数据正确写入到硬盘文件当中
        }catch (IOException e){
            e.printStackTrace();
        }

拷贝文件: 

        //拷贝文件
        try (FileInputStream in =new FileInputStream("test,txt");FileOutputStream out=new FileOutputStream("test2.txt")){
            int s;
            while ((s=in.read())!=-1)
                out.write(s);

            //如果文件很大,加快拷贝速度:
            byte[] bytes2=new byte[1024];
            int len;
            while ((len=in.read(bytes2))!=-1){
                out.write(bytes2,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }

文件字符流

字符流不同于字节,字符流是以一个具体的字符进行读取,因此它只适合读纯文本的文件,如果是其他类型的文件不适用。

字节流;英文1个字节,中文3个字节。 字符流:中英文都是2个字节

    public static void main(String[] args){
        try (FileReader reader = new FileReader("test.txt")){
            System.out.println((char) reader.read());
        }catch (IOException e){
            e.printStackTrace();
        }

        try (FileWriter writer = new FileWriter("test.txt")){
            writer.write("eeee");
        }catch (IOException e){
            e.printStackTrace();
        }

        //拷贝
        try (FileReader reader1 = new FileReader("test.txt");
             FileWriter writer1 = new FileWriter("test1.txt")){
            char[] chars = new char[3];
            int len;
            while ((len=reader1.read(chars))!= -1){
                writer1.write(chars,0,len);
            }
         }catch (IOException e){
            e.printStackTrace();
        }

    }
}

文件对象

    public static void main(String[] args) throws IOException {

        //文件对象
        File file = new File("test.txt");
        System.out.println(file.exists()); //判断文件是否存在
        System.out.println(file.getAbsoluteFile());  //获取绝对路径
        file.createNewFile();  //创建文件
        file.length();  //获取字节长度

        File file1=new File("hello/test2");
        System.out.println(file1.mkdirs()); //创建文件夹

    }
}

    public static void main(String[] args){
        File file=new File("Honor(1).mp3");
        try (FileInputStream in = new FileInputStream("Honor(1).mp3");
             FileOutputStream out =new FileOutputStream("xxx.mp3")){
            byte[]bytes=new byte[1024*1024];
            int len;
            long total =file.length(),sum=0;
            while ((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);
                sum+=len;
                System.out.println("文件已拷贝"+(sum*100/total)+"%");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

/*输出
文件已拷贝10%
文件已拷贝21%
文件已拷贝31%
文件已拷贝42%
文件已拷贝52%
文件已拷贝63%
文件已拷贝73%
文件已拷贝84%
文件已拷贝95%
文件已拷贝100%
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值