栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO类总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO类总结

FileOutputStream的使用:

  1. 获取FileOutputStream对象,有两种方式,
  2. 同时分是否续写
  3. 有三种输出方式
    • fos.write(int b):-----fis.read();
    • fos.write(byte[] byte);-----fis.read(byte[] byte)
    • fos.write(String.getBytes());
  4. 是否换行
  5. 关闭资源
  6. 记得用try…catch…finally来处理整个流程

注意:

  • 为了防止输出失败(各种原因),我们要用try…catch…finally来控制
  • 不管是字节流还是缓冲流,都不推荐单字节读写,一定要用数组字节进行读写,否则巨慢,切记,
  • 字节数组缓冲流读写是速度最快的
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节输出流对象有两种方式
        FileOutputStream fos = new FileOutputStream("1.txt");
        //或者
        FileOutputStream fos1 = new FileOutputStream(new File("1.txt"));
        //输出数据有3种方式
        //1.单字节输出
        fos.write(97);
        //2.多字节数组输出,实际显示会自动转换成我们能看懂多字符
        byte[] bytes = {97,98,99};
        fos.write(bytes);
        //或者指定数组的开始输出索引以及输出长度
        fos.write(bytes,0,bytes.length);
        //3.将字符转成字节数组再输出
        byte[] bytes1 = "我爱你".getBytes(StandardCharsets.UTF_8);
        fos1.write(bytes1);
        //也可以用
        fos1.write("你是谁".getBytes());
        //以上的输出都会直接覆盖掉前面的数据,要想续写或者换行
        FileOutputStream fos3 = new FileOutputStream("1.txt",true);
        FileOutputStream fos4 = new FileOutputStream(new File("1.txt"), true);
        fos3.write(97);
        //用"r"换行
        fos4.write("rn".getBytes());
        fos4.write(98);
        //写完最后要释放资源
        fos.close();
        fos1.close();
    }
}
public class Demo4 {
    public static void main(String[] args){
        //为了保证资源一定会释放,要用finally来控制
        //比如输出失败或者文件不存在
        //正常工作都是要用try...catch来处理异常
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream("1.txt");
            fos.write("我是谁".getBytes());
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            //如果文件不存在fos就是null,自然就不会释放资源,所以要先判断
            if(fos != null){
                try{
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FileInputStream的使用:
1 同样有两种方式去获取字节输入流对象
2 一定要注意读取单字节字符和多字节字符之间的区别
* fis.read();单字节读取,读取后的显示分单字节和多字节字符
* fis.read(byte[] byte);多字节读取
* 两者的区别在于:
* 读取速度的快慢,fis.read(byte[] byte)更快
* fis.read()没法显示多字节字符,但是复制没有问题
3 read()方法返回的是字节数- -整型,要变成字符还得转换类型
4 读取到文件的末尾就会返回-1,用来判断是否读取完文件
5 读取多字节字符然后强制转换显示和直接复制文件的不同要注意

所以读取的时候,分几种情况:
1 如果文本是纯单字节字符组成,比如纯英文的,可以可以用单字节一个一个读取
2 如果文本是多字节字符组成,必须考虑行数和末尾的空格
读取后显示的问题,分几种情况:
1 如果文本是纯单字节字符组成,可以一个一个字节强制转换出来
2 如果文本是多字节字符组成,必须用字符串+数组的方式正确显示出来

public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节流输入对象
        FileInputStream fis = new FileInputStream("1.txt");
        FileInputStream fis1 = new FileInputStream(new File("1.txt"));
        //每次只读取一个字节(这种方法适合英文或者ACII字符集)
        int num = fis.read();//读取出来的是字节而不是字符
        System.out.println(num);
        System.out.println((char)num);//变成字符还得转换类型
        num = fis.read();//要想完全读取就得这样多次读取
        num = fis.read();//文件到达末尾返回-1
        System.out.println(num);
		String s = "中国";
		byte[] by = s.getBytes();
		System.out.println(Arrays.toString(by));
        //释放资源
        fis.close();
    }
}
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节流输入对象
        FileInputStream fis = new FileInputStream("1.txt");
        
        int num = 0;
        while((num = fis.read()) != -1){
            System.out.print(num);
            System.out.println((char)num);
        }
        //释放资源
        fis.close();
    }
}
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节流输入对象
        FileInputStream fis = new FileInputStream("1.txt");
        FileOutputStream fos = new FileOutputStream("2.txt");
        
        int num = 0;
        while((num = fis.read()) != -1){
            fos.write(num);
        }
        //释放资源
        fis.close();
        fos.close();
    }
}
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节流输入对象
        FileInputStream fis = new FileInputStream("1.txt");
        
        byte[] bytes = new byte[1024];//数组的长度是1024
        int len = fis.read(bytes);
        System.out.println(len);
        //这种读取方式有个问题,如果文本是换行的,就会把换行的字符或者空格也全部读取来
        System.out.println(new String(bytes));
        //下面这种读取方式可以避免上面的问题,但是如果文本过大,超过数组读取的长度,
        //就没法读取完整
        System.out.println(new String(bytes,0,len));
        
        
        byte[] bys = new byte[1024];
        int len1;
        while((len1 = fis.read(bys)) != -1){
            System.out.println(new String(bys,0,len1));
        }
        //释放资源
        fis.close();
    }
}

字节缓冲流
BufferOutputStream和BufferedInputStream的底层是用长度为8192的数组,所以效率更高

public class Demo4 {
    public static void main(String[] args) throws IOException {
        //获取字节缓冲流输出对象
        FileOutputStream fos = new FileOutputStream("1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //直接用链式编程
        BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream("1.txt"));
        bos1.write("我爱你呀".getBytes());
        bos1.close();

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.txt"));
        //一次读取一个字节,如果对应文件是多字节的文本,就会遇到字节读取显示问题了,所以不建议这样读取数据
        int by;
        while ((by = bis.read()) != -1){
            System.out.print((char)by);
        }
        //一次读取一个字节数组,建议这样读取数据
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }
    }
}
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //记录开始时间
        long startTime = System.currentTimeMillis();
        //method1();//不知道时间
        //method2();//33318毫秒
        //method3();//167219毫秒
        method4();//7224毫秒
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时" + (endTime - startTime) + "毫秒");

    }

    public static void method1() throws IOException {
        //单字节一次读取一个字节
        FileInputStream fis = new FileInputStream("/1.mov");
        FileOutputStream fos = new FileOutputStream("/2.mov");
        int by;
        while((by=fis.read()) != -1){
            fos.write(by);
        }
        fos.close();
        fis.close();
    }

    public static void method2() throws IOException {
        //一次读写一个字节数组
        FileInputStream fis = new FileInputStream("/1.mp4");
        FileOutputStream fos = new FileOutputStream("/2.mp4");
        byte[] by = new byte[1024];
        int len;
        while((len=fis.read(by)) != -1){
            fos.write(by,0,len);
        }
        fos.close();
        fis.close();
    }

    public static void method3() throws IOException {
        //一次读写一个字节缓冲流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/1.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/2.mp4"));
        int by;
        while((by = bis.read()) != -1){
            bos.write(by);
        }
        bos.close();
        bis.close();
    }

    public static void method4() throws IOException {
        //一次读写一个字节数组缓冲流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/1.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/2.mp4"));
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
    }
}

字符流
由于字节流操作中文不是特别大方便,所以java就提供字符流

  • 字符流 = 字节流 + 编码表
    用字节流复制文本件时,文本文件也有中文,但是没有问题,原因是最终底层操作会自动进行字节拼接成中文。
  • 汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号