首页 > 其他分享 >字节流

字节流

时间:2022-09-21 20:58:06浏览次数:51  
标签:fis 字节 fos len File new null

FileInputStream和FileOutputStream的使用

一,使用字节流FileInputStream处理文本文件(可能异常)

使用同一套结构构建代码:

@Test
    public void testFileInputStream() {
        //使用字节流FileInputStream处理文本文件,可能出现乱码。
        FileInputStream fis = null;
        try {
            //1.造文件
            File file = new File("hello.txt");

            //2.造流
            fis = new FileInputStream(file);

            //3.读数据
            byte[] buffer = new byte[5];
            int len ;//记录每次读取的字节的个数
            while ((len = fis.read(buffer))!= -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis!=null)
            //4.关闭资源
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
二,图片的复制操作
@Test
public void testFileInputOutputStream() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //
        File srcFile = new File("1.jpg");
        File destFile = new File("11.jpg");
        //
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        //复制的过程(字节流)
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos!=null){
        try {

            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
三,指定路径下文件的复制
public void copyFile(String srcPath, String destPath){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        //
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        //复制的过程
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos!=null){
            try {

                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();
        String srcPath = "D:\\BaiduNetdiskDownload\\Anaconda3-2021.05-Linux-x86_64.sh";
        String destPath = "D:\\BaiduNetdiskDownload\\1.sh";
//        String srcPath = "hello.txt";
//        String destPath = "hello3.txt";
        copyFile(srcPath,destPath);

        long end = System.currentTimeMillis();
        System.out.println("复制操纵花费的时间为:"+(end-start));//byte[5] 时间太长 byte[1024] 2788ms
    }
四,结论
  1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
  2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理

标签:fis,字节,fos,len,File,new,null
From: https://www.cnblogs.com/blwx/p/16717100.html

相关文章

  • java中IO流-缓冲流(字节型)复制操作
    importjava.io.*;publicclassBufferedTest{publicstaticvoidmain(String[]args){FileInputStreamfis=null;FileOutputStr......
  • java中IO流字节的读入与复制操作
    importjava.io.*;importorg.junit.Test;/**FileInputStream和FileOutputStream的使用*/publicclassFileInputOutputStreamTest{//使用字节流File......
  • Python学习:len() 函数详解:获取字符串长度或字节数、join()方法:合并字符串
    Python中,要想知道一个字符串有多少个字符(获得字符串长度),或者一个字符串占用多少个字节,可以使用len()函数。语法格式:len(string)string用于指定要进行长度统计的字符串......
  • 大字节数组和 MemoryStream 的替代方案
    发表于2019年12月9日 在.NET中,处理二进制数据时通常使用字节数组;例如,在方法之间传递文件的内容、编码/解码文本、从套接字读取数据等。这些数组可能会变得非常大......
  • String与字符、字节数组之间的转换
    importjava.util.Arrays;publicclassStringTest{publicstaticvoidmain(String[]args)throwsException{//String与字符数组之间的转换......
  • 字节序
    计算机中的数据,一般都是以字节为单位进行存储的。有些数据需要多个字节存储,例如32位整数666(0x0000029A),需要用4个字节来存储。这种数据,一般都是存储在连续的地址......
  • 字节顺序标记
    UTF-32和UTF-16的一个CodeUnit,需要转换成多个字节的序列,因此存在字节序的问题。可以在UTF-32或UTF-16编码的字节流开头,添加字节顺序标记(byte-ordermark,BOM)......
  • 字节跳动 DanceCC 工具链系列之Xcode LLDB耗时监控统计方案
    作者:李卓立仲凯宁背景介绍在《字节跳动DanceCC工具链系列之Swift调试性能的优化方案》[1]一文中,我们介绍了如何使用自定义的工具链,来针对性优化调试器的性能,解决大......
  • idea 查看类的字节码信息( jclassLib Bytecode viewer插件、javap tools)
    idea查看类的字节码信息第一种方式:IntelliJIDEA--Preferences---Plugins安装插件:jclassLibBytecodeviewer第二种方式:IntelliJIDEA--Preferenc......
  • 指针字节落地了
    查指令表就可以知道的,其实,学习指令时就应该有讲述。一般指令格式中没有直接数字(只有寄存器名)的是单字节指令;格式中有一个字节“立即数”或一个字节“地址数”的是双字节指......