IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)

1.File类

File类可以在程序中 操作文件和目录。File类是通过建立File类对象,在调用File类的对象来进行相关操作的。

示例:

  1. public class Demo01 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         File f = new File("f:/我的歌声里.txt");  
  5.         //访问文件名相关  
  6.         String name = f.getName();  
  7.         System.out.println("文件名:" + name);  
  8.         String absolutePath = f.getAbsolutePath();  
  9.         System.out.println("绝对路径:" + absolutePath);  
  10.         String parent = f.getParent();  
  11.         System.out.println("父目录:" + parent);  
  12.           
  13.         //检测相关  
  14.         System.out.println("是否存在:" + f.exists());  
  15.         System.out.println("是否可读" + f.canRead());  
  16.         System.out.println("是否可写:" + f.canWrite());  
  17.           
  18.         //获取文件信息  
  19.         System.out.println("文件的大小: " + f.length());  
  20.           
  21.         //以当前路径创建File对象  
  22.         File file = new File(".");  
  23.         String[] list = file.list();  
  24.         //遍历目录下的文件  
  25.         System.out.println();  
  26.         System.out.println("当前目录下有文件:");  
  27.         for(String name1:list){  
  28.             System.out.println(name1);  
  29.         }  
  30.       
  31.     }  
  32.   
  33. }  

运行结果:


2.IO流的分类

按照方向:输入流和输出流

按照流的大小:字节流和字符流

按照流的角色:节点流和处理流

流的类关系图如下:



3.字节流和字符流

字节流:FileInputStream 和 FileOutputStream

示例:把文件复制成另外的文件

  1. public class Demo02 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         /* 
  5.          * 需求把文件“我的歌声里.txt” 复制并改文件名为 “我的歌声里.java” 
  6.          *  
  7.          */  
  8.     //创建输入流  
  9.      InputStream is = new FileInputStream(new File("f:/我的歌声里.txt"));  
  10.      //创建输出流  
  11.      OutputStream os = new FileOutputStream(new File("f:/我的歌声里.java"));  
  12.      //创建接收字节数组  
  13.      byte[] bytes = new byte[1024];  
  14.      int len = 0;  
  15.      //循环输入输出  
  16.      while((len = is.read(bytes)) != -1 ){  
  17.          os.write(bytes, 0, len);  
  18.      }  
  19.      //关闭资源  
  20.      os.close();  
  21.      is.close();  
  22.   
  23.     }  
  24.   
  25. }  

运行结果:



字符流:FileReader和FileWriter

示例:切割文件

  1. public class Demo03 {  
  2.     public static void main(String[] args) {  
  3.         FileReader r = null;  
  4.         FileWriter w = null;  
  5.         try {  
  6.             int count = 0;//定义一个标记  
  7.             int flag = 0;//文件名标记  
  8.             r = new FileReader("f:/我的歌声里.txt");  
  9.             w = new FileWriter("f:/我的歌声里" + flag +".txt");  
  10.             char[] chars = new char[10];  
  11.             int len = 0;  
  12.             while((len = r.read(chars)) != -1){  
  13.                 System.out.println(new String(chars, 0, len));  
  14.                 w.write(chars, 0, len);  
  15.                 w.flush();  
  16.                 count++;  
  17.                 //定义切割的条件  
  18.                 if(count >10 ){  
  19.                     flag++;  
  20.                     w = new FileWriter("f:/我的歌声里" + flag +".txt");  
  21.                     count = 0;  
  22.                 }  
  23.                   
  24.             }  
  25.               
  26.         } catch (FileNotFoundException e) {  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }finally{  
  32.             try {  
  33.                 w.close();  
  34.                 r.close();  
  35.             } catch (IOException e) {  
  36.                 // TODO Auto-generated catch block  
  37.                 e.printStackTrace();  
  38.             }  
  39.               
  40.         }  
  41.           
  42.   
  43.     }  
  44.   
  45. }  




运行结果


4.转换流

转换流:把字节流转换为字符流,一次来实现性能优化

       InputStreamReader 和 OutputStreamWriter

示例:

  1. public class Demo04 {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws IOException  
  6.      */  
  7.     public static void main(String[] args) throws IOException {  
  8.         //键盘输入到文件  
  9.         InputStreamReader isr = new InputStreamReader(System.in);  
  10.         char[] chars = new char[1024];  
  11.         int len = 0;  
  12.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f:/我的文件.txt"));  
  13.         int count = 0;  
  14.         while((len = isr.read(chars)) != -1){  
  15.             osw.write(chars, 0, len);     
  16.             osw.flush();  
  17.             count++;  
  18.             if(count == 10){  
  19.                 break;  
  20.   
  21.             }  
  22.         }  
  23.         isr.close();  
  24.         osw.close();  
  25.           
  26.   
  27.     }  
  28.   
  29. }  

运行结果:




5.缓冲流

        把流读到缓冲区,然后再一次读到内存中来,以此来提高性能

        BuffererInputStream 和BufferedOutputStream

        BufferedReader 和 BufferedWriter

示例:

  1. public static void main(String[] args) throws IOException {  
  2.           
  3.         //读取并复制保存图片  
  4.         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/qq.jpg")));  
  5.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:/qqq.png")));  
  6.           
  7.         String line = null;  
  8.         while((line = br.readLine()) != null){  
  9.             bw.write(line);  
  10.             bw.flush();  
  11.         }  
  12.           
  13.         bw.close();  
  14.         br.close();  
  15.   
  16.     }  
原文地址:https://www.cnblogs.com/duende99/p/7590802.html