当前位置: 移动技术网 > IT编程>开发语言>Java > 荐 IO流 (二) ----- 文件流

荐 IO流 (二) ----- 文件流

2020年07月17日  | 移动技术网IT编程  | 我要评论
相关文章:
  1. 《IO流 (一) ----- 基本概念和File类》
  2. 《IO流 (二) ----- 文件流》
  3. 《IO流 (三) ----- 字符流和字符缓冲流》
  4. 《IO流 (四) ----- 转换流和标准字节输出流》
  5. 《IO流 (五) ----- 对象数据的序列化和反序列化》


本篇涉及内容


  • FileInputStream详解

  • FileOutputStream详解

  • FileInputStream和FileOutputStream结合使用实现文件拷贝



文件字节输入流FileInputStream详解


一、关于文件字节输入流FileInputStream的基本内容
  1. 能实现的效果:通过该流可以读取硬盘中的文件数据到内存中来。

  2. 创建并且关闭一个文件字节输入流的完整过程

     public static void main(String[] args) {
         
         FileInputStream fis = null;
         
         try {
         	// 绝对路径可以写为:"C:/Users\yukea/Desktop/JavaProject/Test/FileInputStreamTest.txt",也可以用相对路径
             fis = new FileInputStream("C:\\Users\yukea\\Desktop\\JavaProject\\Test\FileInputStreamTest.txt");
             
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } finally {
             if (fis != null) {
                 try {
                     fis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
    
二、FileInputStream的常用方法
  1. 使用int read()读取文件中的数据——返回读取到的字节本身

    try {
    		 //.txt文件中存储了abc三个字符,在windows中占用3个字节空间
             fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
          
             fis.read();// 返回97
             fis.read();// 返回98
             fis.read();// 返回99
             fis.read();// 返回-1
         } 
    

    原理:有一个指针会随着Read()方法的调用而前移,首次调用Read()方法时指针前移指向.txt文件中的第一个字节,调用完后返回该字节数,后面每调用一次Read()方法则指针向后移动一位并且返回对应的字节数,直到指针指向文件末尾时返回-1。

  2. 使用int read(byte[] b)读取文件中的数据——返回读取到的字节数量

    try {
    	     //.txt文件中存储了abcdef五个个字符
             fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
             
             byte[] bytes = new byte[4];
    
             fis.read(bytes);// 返回4,读取到了4个字节到bytes数组中
             System.out.println(new String(bytes));// 打印 "abcd" 字符串
             
             fis.read(bytes);// 返回2,读取到了2个字节到bytes数组中
             System.out.println(new String(bytes));// 打印 "efcd" 字符串
             
             fis.read(bytes);// 返回-1,没有读取到其他字节
         } 
    

    原理:每次最多读取bytes数组长度的字节。
    第一次读取:{97, 98 ,99 ,100} 4个字节保存到了字节数组中,返回读取到的字节数量4
    第二次读取:{101, 102, 99, 100} 文件中的后两个字节覆盖到了字节数组中,返回读取到的字节数量2
    第三次读取:返回-1,因为已读取到文件末尾

  3. int read(byte[] b)结合String构造方法使用——读取多少字节转换为多长字符串

       try {
    	     //.txt文件中存储了abcdef五个个字符
             fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
             
             byte[] bytes = new byte[4];
             int readCount = 0;
    
             readCount = fis.read(bytes);// 返回4,读取到了4个字节到bytes数组中
             System.out.println(new String(bytes, 0, readCount));// 打印 "abcd" 字符串
             
             readCount = fis.read(bytes);// 返回2,读取到了2个字节到bytes数组中
             System.out.println(new String(bytes, 0, readCount));// 打印 "ef" 字符串
             
         } 
    
  4. int available()——返回文件中剩余的没有读取到的字节数量

  5. long skip(long n)——跳过几个字节不读取

  6. int available()配合int read(byte[] b)——不使用循环,一次性读取完文本中的字节。

    try {
    
             fis = new FileInputStream("Test/FileInputStreamTest.txt");
    
             // 创建一个长度恰好为文本字节数量的bytes数组
             byte[] bytes = new byte[fis.available()];
    
             // 把文件中的字节读取到bytes数组中
             fis.read(bytes);
    
             // bytes数组转为字符串
             String s = new String(bytes);
    
             System.out.println(s);// 输出"莫莫你好hello"
    
         }
    
三、从硬盘上读取一个.txt文件内容到内存中的流程
  1. 先创建一个.txt文件

  2. 代码实现——将文件内容保存到一个String字符串上

         FileInputStream fis = null;
         try {
             fis = new FileInputStream("Test/FileInputStreamTest.txt");
             byte[] bytes = new byte[12];
             int readCount = 0;
    
             StringBuffer sb = new StringBuffer(10);
             
             while ((readCount = fis.read(bytes)) != -1) {
                 String s = new String(bytes, 0, readCount);
                 sb.append(s);
             }
             System.out.println(sb);// 打印 "莫莫你好hello"
    
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if (fis != null) {
                 try {
                     fis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
    


文件字节输出流FileOutputStream详解


一、关于文件字节输出流FileOutputStream的基本内容
  1. 能实现的效果:通过该流可以将内存中的数据写入到硬盘数据文件中去。

  2. 创建并且关闭一个文件字节输出流的完整过程

    try {
     		 // 如果文件不存在则会自动新建一个文件
             fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
    
         } catch (FileNotFoundException e) {
             e.printStackTrace();
    
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if (fos != null) {
                 try {
                     fos.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
    
二、FileOutputSream的常用方法
  1. 将指定字节数组中的字节写到流中的方法
    (1) void write(byte[] b)
    (2) void write(byte[] b, int off, int len)

    try {
            fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
    
             byte[] bytes = {97, 98, 99, 100};
             
             fos.write(bytes);// 将数组中的全部字符写入
     		 fos.write(bytes, 0, 2);// 将数组中从下标0开始写入2个长度的字节(接着上面写入的字节后面)
    
             // 写完后一定要刷新!
             fos.flush();
             // 刷新后该文本文件中出现abcdab
         }
    

    注意:使用上述方法将字节写入文件,写入之前会先清空文件的内容再进行写入。
    以追加的方式写入:fos = new FileOutputStream("Test/FileOutputStreamTest.txt", ture);

  2. 配合String类的byte[] getBytes()方法使用——将字符串转换为字节数组,向文件中写入字符串

            try {
             FileOutputStream fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
    
             String s = "莫莫大包子";
             fos.write(s.getBytes());
             fos.flush();
    
         } 
    


FileInputStream和FileOutputStream的结合使用实现文件拷贝


        String originFilePath = "/Users/apple/Downloads/FinalVideo_1591626463.139785.MOV";
        String targetFilePath = "Test/copyFile/copiedFile2.MOV";
        
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(originFilePath);
            fos = new FileOutputStream(targetFilePath);

            // 一次读取1MB 
            byte[] bytes = new byte[1024 * 1024];
            int readCount = 0;

            // 读取多少写入多少
            while ((readCount = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, readCount);
            }

            fos.flush();

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


本文地址:https://blog.csdn.net/k909397116/article/details/107363246

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网