`

IO流

 
阅读更多

1.IO流是干什么的?

 

IO流指 的是输入输出流,用来处理设备上的数据。这里的设备指硬盘,内存,键盘录入,网络传输等。

2.IO流的分类?

按处理数据类型来分:字节流和字符流

按流的方向来分:输入流和输入流。

PS:初学流的时候,新手很容易搞不清什么时候用输入流,什么时候用输出流。简单来说,当需要读数据的时候,需要使用输入流,当需要写数据的时候,需要使用输出流。我以前是这么记忆的:“读入写出”,要读的话就用输入流,要写的话,就用输出流。经常想起这4个字,慢慢就记熟下来了。

3.什么时候使用字节流?什么时候使用字符流?

首先需要知道的是,任何数据存在硬盘上时,都是以二进制的形式存储的。而通过使用字节流,可以读取任意文件。字节流一次读取一个字节,而字符流使用了字节流读到一个或者多个字节时,去查找指定的编码表,返回对应的编码。所以字符流只能处理纯文本字符数据,而字节流可以处理更多类型的数据,比如图片,视频,音频文件等。因此,只要是纯文本数据处理,优先考虑使用字符流。其他情况就使用字节流。

4.IO流类的关系(列举了其中一部分)

 

1.字符流的读写+不加BufferedReader/Writer

Java代码 复制代码 收藏代码
  1. public class CharIOTest {   
  2.        
  3.     /**   
  4.       * 流转换读取数据.  
  5.      * @throws FileNotFoundException   
  6.      * @throws IOException   
  7.       * @throws IOException   
  8.       */    
  9.     @Test  
  10.      public void readChangeTest() throws IOException  {     
  11.          File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件   
  12.          FileInputStream fis = new FileInputStream(file);// 获取该文件的输入流    
  13.          InputStreamReader isr=new InputStreamReader(fis, "gbk");   
  14.          char[] bb = new char[1024];// 用来保存每次读取到的字符     
  15.          StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好     
  16.          int n;// 每次读取到的字符长度     
  17.          while ((n = isr.read(bb)) != -1) {    
  18.              sb.append(new String(bb, 0, n));     
  19.          }     
  20.          isr.close();// 关闭输入流,释放连接     
  21.          System.out.println(sb);     
  22.      }     
  23.        
  24.     /**  
  25.      * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。  
  26.      * 好处:代码少写几行,但不通用。  
  27.      * @throws IOException  
  28.      */  
  29.     @Test  
  30.      public void readTest() throws IOException  {     
  31.          File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要读取的文件   
  32.             
  33.          FileReader fr=new FileReader(file);   
  34.  //        System.out.println(fr.getEncoding());   
  35.          char[] bb = new char[1024];// 用来保存每次读取到的字符     
  36.          StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好     
  37.          int n;// 每次读取到的字符长度     
  38.          while ((n = fr.read(bb)) != -1) {    
  39.              sb.append(new String(bb, 0, n));     
  40.          }     
  41.          fr.close();// 关闭输入流,释放连接     
  42.          System.out.println(sb);     
  43.      }   
  44.        
  45.      /**   
  46.       * 往文件里写入数据.   
  47.       * @throws IOException   
  48.       */  
  49.     @Test  
  50.      public void writeTest() throws IOException {     
  51.          String writerContent = "hello world,你好世界";// 要写入的文本     
  52.          File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 要写入的文本文件     
  53.          if (!file.exists()) {// 如果文件不存在,则创建该文件     
  54.              file.createNewFile();     
  55.          }     
  56.             
  57.          FileWriter writer = new FileWriter(file);// 获取该文件的输出流   
  58.          System.out.println(writer.getEncoding());   
  59.          // 写内容  ,默认使用writer.getEncoding()进行写入,不会出现乱码,因为得到的文件的编码便是写入的编码   
  60.          writer.write(writerContent);   
  61.          writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里     
  62.          writer.close();// 关闭输出流,施放资源     
  63.      }    
  64.  }  
public class CharIOTest {
 	
 	/** 
      * 流转换读取数据.
 	 * @throws FileNotFoundException 
 	 * @throws IOException 
      * @throws IOException 
      */ 
 	@Test
     public void readChangeTest() throws IOException  {  
         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
         FileInputStream fis = new FileInputStream(file);// 获取该文件的输入流 
         InputStreamReader isr=new InputStreamReader(fis, "gbk");
         char[] bb = new char[1024];// 用来保存每次读取到的字符  
         StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
         int n;// 每次读取到的字符长度  
         while ((n = isr.read(bb)) != -1) { 
             sb.append(new String(bb, 0, n));  
         }  
         isr.close();// 关闭输入流,释放连接  
         System.out.println(sb);  
     }  
 	
 	/**
 	 * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
 	 * 好处:代码少写几行,但不通用。
 	 * @throws IOException
 	 */
 	@Test
     public void readTest() throws IOException  {  
         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要读取的文件
         
         FileReader fr=new FileReader(file);
 //        System.out.println(fr.getEncoding());
         char[] bb = new char[1024];// 用来保存每次读取到的字符  
         StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
         int n;// 每次读取到的字符长度  
         while ((n = fr.read(bb)) != -1) { 
             sb.append(new String(bb, 0, n));  
         }  
         fr.close();// 关闭输入流,释放连接  
         System.out.println(sb);  
     }
 	
 	 /** 
      * 往文件里写入数据. 
      * @throws IOException 
      */
 	@Test
     public void writeTest() throws IOException {  
         String writerContent = "hello world,你好世界";// 要写入的文本  
         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 要写入的文本文件  
         if (!file.exists()) {// 如果文件不存在,则创建该文件  
             file.createNewFile();  
         }  
         
         FileWriter writer = new FileWriter(file);// 获取该文件的输出流
         System.out.println(writer.getEncoding());
         // 写内容  ,默认使用writer.getEncoding()进行写入,不会出现乱码,因为得到的文件的编码便是写入的编码
         writer.write(writerContent);
         writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里  
         writer.close();// 关闭输出流,施放资源  
     } 
 }

2.字节流的读写+不加BufferedInputStream/OutputStream

Java代码 复制代码 收藏代码
  1. public class ByteIOTest {   
  2.     /**   
  3.       *    
  4.       * 将D盘下的D:\\xxx.ico文件,读取后,再存到E盘下面.   
  5.       *    
  6.       * @param args   
  7.       * @throws Exception   
  8.       */     
  9.     @Test  
  10.      public void imageIOTest(String[] args) throws Exception {     
  11.          FileInputStream in = new FileInputStream(new File("D:\\xxx.ico"));// 指定要读取的图片     
  12.          File file = new File("E:\\test.jpg");     
  13.          if (!file.exists()) {// 如果文件不存在,则创建该文件     
  14.              file.createNewFile();     
  15.          }     
  16.          FileOutputStream out = new FileOutputStream(new File("E:\\yuanwang.ico"));// 指定要写入的图片     
  17.          int n = 0;// 每次读取的字节长度     
  18.          byte[] bb = new byte[1024];// 存储每次读取的内容     
  19.          while ((n = in.read(bb)) != -1) {     
  20.              out.write(bb, 0, n);// 将读取的内容,写入到输出流当中     
  21.          }     
  22.          out.close();// 关闭输入输出流     
  23.          in.close();     
  24.      }     
  25.  }  
public class ByteIOTest {
 	/** 
      *  
      * 将D盘下的D:\\xxx.ico文件,读取后,再存到E盘下面. 
      *  
      * @param args 
      * @throws Exception 
      */  
 	@Test
     public void imageIOTest(String[] args) throws Exception {  
         FileInputStream in = new FileInputStream(new File("D:\\xxx.ico"));// 指定要读取的图片  
         File file = new File("E:\\test.jpg");  
         if (!file.exists()) {// 如果文件不存在,则创建该文件  
             file.createNewFile();  
         }  
         FileOutputStream out = new FileOutputStream(new File("E:\\yuanwang.ico"));// 指定要写入的图片  
         int n = 0;// 每次读取的字节长度  
         byte[] bb = new byte[1024];// 存储每次读取的内容  
         while ((n = in.read(bb)) != -1) {  
             out.write(bb, 0, n);// 将读取的内容,写入到输出流当中  
         }  
         out.close();// 关闭输入输出流  
         in.close();  
     }  
 }

3.字符流的读写+加BufferedReader/Writer

Java代码 复制代码 收藏代码
  1. public class BufferCharIOTest {   
  2.     /**   
  3.       * 考虑到编码问题-转换流读取,即字节流->字符流  
  4.           windows系统下默认编码为gbk,即所读取文本为gbk编码。  
  5.                             因此如果myeclipse的默认是utf-8编码的话则读取中文时会出现乱码  
  6.                             情况,utf8 中文会默认3个字节,而gbk 中文默认2个字节。  
  7.                           一个字符在不同编码的情况下,其所对应的字节数是不相同,字符有中文字符,英文字符,数字字符等等   
  8.       * @throws FileNotFoundException   
  9.       * @throws IOException   
  10.       */  
  11.     @Test  
  12.      public void readChangeTest() throws FileNotFoundException, IOException {     
  13.          File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件     
  14.          // 获得该文件的缓冲输入流     
  15.          //BufferedReader bufferedReader = new BufferedReader(new FileReader(file));     
  16.             
  17.          BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));   
  18.          InputStreamReader isr=new InputStreamReader(bis, "gbk");   
  19.             
  20.          char[] bb = new char[1024];// 用来保存每次读取到的字符     
  21.          StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好     
  22.          int n;// 每次读取到的字符长度     
  23.          while ((n = isr.read(bb)) != -1) {    
  24.              sb.append(new String(bb, 0, n));     
  25.          }     
  26.          isr.close();// 关闭输入流,释放连接     
  27.          System.out.println(sb);     
  28.      }     
  29.        
  30.        
  31.     /**  
  32.      * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。  
  33.      * 好处:代码少写几行,但不通用。  
  34.      * @throws IOException  
  35.      */  
  36.     @Test  
  37.      public void readTest() throws FileNotFoundException, IOException {     
  38.         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件     
  39.          // 获得该文件的缓冲输入流     
  40.          BufferedReader bufferedReader = new BufferedReader(new FileReader(file));     
  41.          String line = "";// 用来保存每次读取一行的内容     
  42.          while ((line = bufferedReader.readLine()) != null) {     
  43.              System.out.println(line);     
  44.          }     
  45.          bufferedReader.close();// 关闭输入流   
  46.      }     
  47.       
  48.      /**   
  49.       * 在写入过程中,不需要考虑编码问题,  
  50.       * 因为生成的文件文本的编码会与写入编码一致。因此不会出现乱码情况  
  51.       *    
  52.       * @throws IOException   
  53.       */  
  54.     @Test  
  55.      public void writeTest() throws IOException {     
  56.          File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要写入的文件     
  57.          if (!file.exists()) {// 如果文件不存在则创建     
  58.              file.createNewFile();     
  59.          }     
  60.          // 获取该文件的缓冲输出流     
  61.          BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));     
  62.          // 写入信息     
  63.          bufferedWriter.write("你好世界");     
  64.          bufferedWriter.newLine();// 表示换行     
  65.          bufferedWriter.write("hello world");     
  66.          bufferedWriter.flush();// 清空缓冲区     
  67.          bufferedWriter.close();// 关闭输出流     
  68.      }     
  69.  }  
public class BufferCharIOTest {
 	/** 
      * 考虑到编码问题-转换流读取,即字节流->字符流
     	  windows系统下默认编码为gbk,即所读取文本为gbk编码。
                            因此如果myeclipse的默认是utf-8编码的话则读取中文时会出现乱码
                            情况,utf8 中文会默认3个字节,而gbk 中文默认2个字节。
                          一个字符在不同编码的情况下,其所对应的字节数是不相同,字符有中文字符,英文字符,数字字符等等 
      * @throws FileNotFoundException 
      * @throws IOException 
      */
 	@Test
     public void readChangeTest() throws FileNotFoundException, IOException {  
         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件  
         // 获得该文件的缓冲输入流  
         //BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
         
         BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
         InputStreamReader isr=new InputStreamReader(bis, "gbk");
         
         char[] bb = new char[1024];// 用来保存每次读取到的字符  
         StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
         int n;// 每次读取到的字符长度  
         while ((n = isr.read(bb)) != -1) { 
             sb.append(new String(bb, 0, n));  
         }  
         isr.close();// 关闭输入流,释放连接  
         System.out.println(sb);  
     }  
 	
 	
 	/**
 	 * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
 	 * 好处:代码少写几行,但不通用。
 	 * @throws IOException
 	 */
 	@Test
     public void readTest() throws FileNotFoundException, IOException {  
 		File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件  
         // 获得该文件的缓冲输入流  
         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
         String line = "";// 用来保存每次读取一行的内容  
         while ((line = bufferedReader.readLine()) != null) {  
             System.out.println(line);  
         }  
         bufferedReader.close();// 关闭输入流
     }  
   
     /** 
      * 在写入过程中,不需要考虑编码问题,
      * 因为生成的文件文本的编码会与写入编码一致。因此不会出现乱码情况
      *  
      * @throws IOException 
      */
 	@Test
     public void writeTest() throws IOException {  
         File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要写入的文件  
         if (!file.exists()) {// 如果文件不存在则创建  
             file.createNewFile();  
         }  
         // 获取该文件的缓冲输出流  
         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));  
         // 写入信息  
         bufferedWriter.write("你好世界");  
         bufferedWriter.newLine();// 表示换行  
         bufferedWriter.write("hello world");  
         bufferedWriter.flush();// 清空缓冲区  
         bufferedWriter.close();// 关闭输出流  
     }  
 }

4.字节流的读写+加BufferedInputStream/OutputStream

Java代码 复制代码 收藏代码
  1. public class BufferByteIOTest {   
  2.     public void imageIOTest(String[] args) throws Exception {     
  3.          // 指定要读取文件的缓冲输入字节流     
  4.          BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\xx.jpg"));     
  5.          File file = new File("E:\\xx.jpg");     
  6.          if (file != null) {     
  7.              file.createNewFile();     
  8.          }     
  9.          // 指定要写入文件的缓冲输出字节流     
  10.          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));     
  11.          byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组     
  12.          int n;// 每次读取到的字节数组的长度     
  13.          while ((n = in.read(bb)) != -1) {     
  14.              out.write(bb, 0, n);// 写入到输出流     
  15.          }     
  16.          out.close();// 关闭流     
  17.          in.close();     
  18.      }     
  19.  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics