Io输入输出流---节点流(文件流)

视频参考:05-尚硅谷-Java语言高级-IO流概述与流的分类哔哩哔哩bilibili

视频参考:https://www.bilibili.com/video/BV114411W7id?p=57&spm_id_from=pageDriver&vd_source=cb0d3da7202ac90dd1d32ce6960fb3b7

 

IO输入输出流

File

package com.mokuiran.file;

import java.io.File;
import java.io.IOException;

public class DemoFile {
   public static void main(String[] args) {
       //file可以表示一个不存在的文件
       File file = new File("E:\\test.txt");

       System.out.println(file.isFile()==true?"文件":"非文件");
       System.out.println(file.isDirectory()==true?"目录":"非目录");
       System.out.println("相对路径:"+file.getPath());
       System.out.println("绝对路径:"+file.getAbsolutePath());
       System.out.println("文件名称:"+file.getName());
       System.out.println("文件大小:"+file.length());

       //判断文件是否存在
       boolean exists = file.exists();

       //若存在则删除,若不存在则创建
//       if (exists){
//           file.delete();//删除文件
//           //彻底删除,删除的文件不经过回收站
//           System.out.println("删除成功!!");
//       }else {
//           try {
//               file.createNewFile();
//           } catch (IOException e) {
//               e.printStackTrace();
//           }
//       }

  }
}
 

读入文件

package com.mokuiran.file;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class DemoReader {
   public static void main(String[] args) throws IOException {
       File file1 = new File("hello.txt");//相较于当前工程
       System.out.println(file1.getAbsolutePath());


       DemoReader demo = new DemoReader();
       System.out.println("普通读入:");
       demo.testFileReader();
       System.out.println();
       System.out.println("数组式的读入:");
       demo.testReader2();

  }
       //单元测试
       @Test
       public void testFileReader() throws IOException {
           //1.实例化file类的对象,指明要操作的文件
           File file1 = new File("src\\com\\mokuiran\\file\\hello.txt");
           //2.提供具体的流
           FileReader fr = new FileReader(file1);
           //3.数据读入
           //read():返回读入的一个字符,如果达到文件末尾,返回-1
           //方式一:
//           int data = fr.read();
//           while (data!=-1){
//               System.out.print((char) data);
//               data = fr.read();
//           }

           //方式二:
           int data;
           while ((data = fr.read())!=-1){
               System.out.print((char)data);
          }
           //流的关闭
           fr.close();

      }

       public void testReader2() {
           //1.File类的实例化
           File file = new File("src\\com\\mokuiran\\file\\hello.txt");

           FileReader fr = null;


           try {
               //2.FileReader流的实例化
               fr = new FileReader(file);


               //3.读入的操作
               //read(char[] cb):返回每次读入cb数组中的字符个数。如果达到文件末尾,返回-1
               char[] cb = new char[5];
               int len;

               //错误写法1
//           while ((len = fr.read(cb))!=-1){
//               for (int i = 0;i < cb.length;i++){
//                   System.out.print(cb[i]);
//               }
//           }
               //正确写法1
//           while ((len = fr.read(cb))!=-1){
//               for (int i = 0;i < len;i++){
//                   System.out.print(cb[i]);
//               }
//           }

               //错误写法2
//           while ((len = fr.read(cb))!=-1){
//               String string = new String(cb);
//               System.out.println(string);
//           }

               //正确写法2
               while ((len = fr.read(cb))!=-1){
                   String string = new String(cb,0,len);
                   System.out.println(string);
              }
          } catch (IOException e) {
               e.printStackTrace();
          }finally {
               //4.资源的关闭
               try {
                   if (fr!=null){
                       fr.close();
                  }
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
}


 

写入文件

package com.mokuiran.file;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DemoWriter {
   public static void main(String[] args) throws IOException {
       DemoWriter dw = new DemoWriter();
       dw.TestFileWriter();
  }
   //从内存中写出数据到硬盘文件里
   /*
       1.输出操作,对应的File可以不存在。
       如果不存在,在输出的过程中,会自动创建文件
       如果存在,覆盖原文件的数据
   */
   
   public void TestFileWriter(){
           FileWriter fw = null;
       try {
           //1.提供File类的对象,指明衔写出到的文件
           File file = new File("hello.txt");

           //2.提供FileWriter的对象,用于数据的写出
           fw = new FileWriter(file,true);//若第二个参数为true,则为追加数据,否则为覆盖数据

           //3.写出的操作
           fw.write("I have a dream!\n");
           fw.write("l love China\n");


      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           try {
               if (fw!=null){
                   //4.流资源的关闭
                   fw.close();
              }
          } catch (IOException e) {
               e.printStackTrace();
          }

      }
  }
}

 

利用FileReader()与FileWriter()进行文本文件的复制

package com.mokuiran.file;

import java.io.*;

public class DemoRW {
   public static void main(String[] args) {

       DemoRW demoRW = new DemoRW();
       demoRW.TestRww();
  }

   public void TestRww() {
       FileReader fr = null;
       FileWriter fw = null;
       try {
           //1.创建对象
           File fileR = new File("hello.txt");
           File fileW = new File("hello2.txt");


           //2.创建流
           fr = new FileReader(fileR);
           fw = new FileWriter(fileW);


           //3.数据的读入和写入操作
           char[] c = new char[5];
           int len;//记录每次读入到c输出中的字符的个数
           while ((len = fr.read(c)) != -1){
               //每次写出len个字符
               fw.write(c,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //4.数据流的关闭
           try {
               if (fw!=null){
                   fw.close();
              }
          } catch (IOException e) {
               e.printStackTrace();
          }
           try {
               if (fr!=null){
                   fr.close();
              }
          } catch (IOException e) {
               e.printStackTrace();
          }
      }
  }
}

 

利用FileInputStream和FileOutputStream进行非文本文件的复制

package com.mokuiran.file;

import java.io.*;

/**
* 图片的复制以及视频的复制都可以运用到
*/

public class DemoCopyImage {
   public static void main(String[] args) {
       DemoCopyImage dci = new DemoCopyImage();
       try {
           dci.TestCopy();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
   public void TestCopy() {
       FileInputStream fr = null;
       FileOutputStream fw = null;
       try {
           //创建File对象
           File file = new File("1.jpg");//被复制的图片名
           File file1 = new File("新图片2.jpg");//复制后新的图片名

           //创建流
           fr = new FileInputStream(file);
           fw = new FileOutputStream(file1);

           //数据操作
           byte[] bytes = new byte[5];
           int len;
           while ((len = fr.read(bytes))!=-1){
               fw.write(bytes,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //关闭资源
           try {
               if (fw!=null){
                   fw.close();
              }
          } catch (IOException e) {
               e.printStackTrace();
          }
           try {
               if (fr!=null){
                   fr.close();
              }
          } catch (IOException e) {
               e.printStackTrace();
          }
      }
  }
}

 

上述代码都会有一些类似或者相同的代码,因此我们将这些代码进行整合,放入到一个方法里,当我们需要时进行调用,代码如下:

package com.mokuiran.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DemoMethod {
   //srcPath为将要复制文件的路径,destPath为复制的文件的路径
   public void FileMethod(String srcPath,String destPath){
       FileInputStream fi = null;
       FileOutputStream fo = null;
       try {
           //1.创建File对象
           File file = new File(srcPath);
           File file1 = new File(destPath);

           //2.创建流
            fi = new FileInputStream(file);
            fo = new FileOutputStream(file1);

           //3.操作数据
           int len;
           byte[] bytes = new byte[1024];
           while ((len=fi.read(bytes))!=-1){
               fo.write(bytes,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //关闭资源
           if (fo!=null){
               try {
                   fo.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (fi!=null){
               try {
                   fi.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }


}

 

创建测试类,进行测试方法

package com.mokuiran.file;

public class Test {
   public static void main(String[] args) {
       DemoMethod demoMethod = new DemoMethod();

       String p1 = "C:\\test.txt";
       String p2="C:\\test\\test.txt";

       demoMethod.FileMethod(p1,p2);//调用方法,将参数传入到方法当中
  }
}

 

结论

对于读文件来说:

1.对于文本文件(.txt,.java,.c,.cpp),使用字符流处理

2.对于非文本文件(.jpg,.mp3,.mp4,.ovi,.doc,.ppt...),使用字节流处理

对于复制文件来说

无论是文本还是非文本文件,都可以使用字节流相关代码进行复制

posted @   默夔然  阅读(89)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示