Java IO流

1、字节输入流

1.1 FileInputStream概述

FileInputStream时用来读取文件数据的字节输入流

1.2FileInputStream对象创建

    public static void main(String[] args) throws FileNotFoundException {
      
        FileInputStream fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");
        System.out.println(fils);
    }

1.3 读取数据

1.3.1 一次读取一个字节(当返回值为-1时没有内容)

    public static void main(String[] args) throws IOException {
        FileInputStream fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");

        //读取数据(字节)
        int b;
        while ((b = fils.read()) != -1){
            System.out.println(b);
        }

        //释放资源
        fils.close();
    }

1.3.2一次读取一个字节数组的

传入一个字节数组,最多读取一个字节数组的数组,并且会把数据存入数组中,返回值代表本次读取到的字节的数量。(返回值为-1都到末尾) 

    public static void main(String[] args) throws IOException {
        FileInputStream fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");

        //读取数据(字节)
        byte[] bytes = new byte[5];
        int len = fils.read(bytes);

        while ((len = fils.read(bytes)) != -1){
            String s = new String(bytes, 0, len);
            //String s = new String(bytes);
            System.out.println(s);
        }

        //释放资源
        fils.close();
    }

其中len的作用:防止读取过程中最后剩余的字节数小于字节数组的长度时,剩余的部分只会覆盖上一个数组的前半部分,出现读取错误。

1.4资源释放(异常抛出)

1.4.1 JDK6版本

public class IO {
    public static void main(String[] args) {
        FileInputStream fils = null;
        try {
            fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");

            //读取数据(字节)
            byte[] bytes = new byte[1024];
            int len;

            while ((len = fils.read(bytes)) != -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try {
                fils.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

1.4.1 JDK7版本

在try的后面可以加小括号,把需要释放的资源在小括号中定义。jvm会帮我们在最后调用close释放资源。

public class IO {
    public static void main(String[] args) {
        try (FileInputStream fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");){
            //读取数据(字节)
            byte[] bytes = new byte[1024];
            int len;

            while ((len = fils.read(bytes)) != -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1.4.2 JDK9版本

public class IO {
    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fils = new FileInputStream("C:\\Users\\HP\\Desktop\\kucha\\test.txt");
        try (fils){
            //读取数据(字节)
            byte[] bytes = new byte[1024];
            int len;

            while ((len = fils.read(bytes)) != -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、 字节输出流

所有的字节输出流父类都是Java.io.OutputStream,他是以字节为单位的输出流

2.1 FileOutputDream对象创建

public class IO {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("C:\\Users\\HP\\Desktop\\Quecha\\text.txt");
        FileOutputStream fos = new FileOutputStream(file);
    }
}

2.2 写数据

2.1.1 一次写一个字节

public class IO {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\HP\\Desktop\\kucha\\text.txt");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write('j');
        fos.close();
    }
}

2.1.2 写入数组

public class IO {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\HP\\Desktop\\kucha\\text.txt");
        FileOutputStream fos = new FileOutputStream(file);

        byte[] bytes = "jia".getBytes();
        fos.write(bytes);                  //写入数组
        fos.write(bytes, 0,2);     //写入数组一部分
        fos.close();
    }
}

2.3 文件续写

之前的构造方法创建对象流,每次流对象创建的时候就会把文件中的内容清空。所以没有办法实现续写。

public class IO {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\HP\\Desktop\\kucha\\text.txt");
        FileOutputStream fos = new FileOutputStream(file,true);

        byte[] bytes = "jia".getBytes();
        fos.write(bytes);                  //写入数组
        fos.write(bytes, 0,2);     //写入数组一部分
        fos.close();
    }
}

3.IO流应用

3.1文件复制

public class IO {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\HP\\Desktop\\kucha\\text.txt");
        File file1 = new File("C:\\Users\\HP\\Desktop\\kucha\\chaku");

        File file2 = new File(file1, file.getName());

        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file2,true);

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值