IO流学习总结

 

一、 流的分类

  按数据流动方向

输入流:只能从中读取字节数据,而不能向其写出数据

输出流:只能向其写入字节数据,而不能从中读取数据

  按照流所处理的数据类型

字节流:用于处理字节数据。

字符流:用于处理字符数据。

  按照流所处理的源

节点流:/向一个特定的IO设备读/写数据的流。(低级流)

处理流:对已存在的流进行连接和封装的流。(高级流)

二、 缓冲流

  缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。

  Java提供了四种缓存流:

BufferedReader

BufferedWriter

BufferedInputStream

BufferedOutputStream

  缓冲输入流支持其父类的mark()reset()方法:

mark()用于“标记”当前位置,就像加入了一个书签,可以使用reset()方法返回这个标记重新读取数据。

  BufferedReader提供了readLine()方法用于读取一行字符串(以\r\n分隔)。

   BufferedWriter提供了newLine()用于写入一个行分隔符。

  对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()方法将会使内存中的数据立刻写出。

三、 类层次

3.1InputStream类层次

 

3.2OutputStream类层次

3.3Reader类层次

 

3.4Writer类层次

 

 

四、 常用的字符流与字节流的转化

说明:

1.     字节流用于读写诸如图像数据之类的原始字节流。

2.     字符流用于读写诸如文件数据之类的字符流。

3.     低级流能和外设交流。

4.     高级流能提高效率。

5.InputStreamReader 是字节流通向字符流的桥梁。

6.OutputStreamWriter是字符流通向字节流的桥梁。

 

五、 代码实例

5.1、常用读文件:

/**

    *以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

    *@paramfileName:文件的名

    */

   publicstaticvoid readFileByBytes(String fileName) {

      File file= new File(fileName);

      InputStream in = null;

      try {        

 /*一次读多个字节*/

          byte[] buf =newbyte[1024];

          int num = 0;

          in = new FileInputStream(file);

           /*读入多个字节到字节数组中,num为一次读入的字节数*/

          while ((num = in.read(buf)) != -1) {

             System.out.print(buf);

          }

      } catch (Exception e1) {

         throw newRuntimeException("读取文本文件异常");

      } finally {

          if (in !=null) {

             try {

                 in.close();

             } catch (IOException e1) {

                 throw newRuntimeException("读取文本文件异常");

             }

          }

      }

    }

/**

    *以字符为单位读取文件,常用于读文本,数字等类型的文件

    *@paramfileName:文件名

    */

   publicstaticvoid readFileByChars(StringfileName) {

      Reader reader = null;

      try {

          System.out.println("以字符为单位读取文件内容,一次读多个字节:");

           /*一次读多个字符*/

          char[] buf =newchar[100];

          int num = 0;

          if(fileName!=null&&!"".equals(fileName)){

             reader = new InputStreamReader(new FileInputStream(fileName));

              /*读入多个字符到字符数组中,num为一次读取字符数*/

             while ((num = reader.read(buf)) != -1) {

                     System.out.print(newString(buf,0,num));

                 }

             }

          }

      } catch (Exception e1) {

          throw newRuntimeException ("读取文本文件异常");

      } finally {

          if (reader !=null) {

             try {

                 reader.close();

             } catch (IOException e1) {

                 throw newRuntimeException ("读取文本文件异常");          }

          }

      }

   }

/**

    *以行为单位读取文件,常用于读面向行的格式化文件

    *@paramfileName:文件名

    */

   publicstatic List<String> readFileByLines(StringfileName) {

      if(fileName!=null&&!"".equals(fileName)){

          File file = new File(fileName);

          BufferedReader reader= null;

          try {

              System.out.println("以行为单位读取文件内容,一次读一整行:");

              reader = new BufferedReader(new FileReader(file));

              String line = null;

              /*一次读入一行,直到读入null为文件结束*/

              while ((line =reader.readLine()) != null) {

                 System.out.println(line);

              }

          } catch (IOException e) {

              throw new RuntimeException("读取文本文件异常");

          } finally {

              if (reader !=null) {

                 try {

                     reader.close();

                 } catch (IOException e1) {

                     throw new RuntimeException("读取文本文件异常");

                 }

              }

          }

       }

} 

5.2、常用写文件:

/** 

   *把内容写到文件

   *@paramfilePathName文件名  

   */ 

   publicstaticboolean writerFile(String filePathName,String content){

      boolean flag=false;

      OutputStreamWriter osw=null;

      try {

          if(filePathName!=null&&!"".equals(filePathName)){

             osw = new OutputStreamWriter(new FileOutputStream(filePathName));

          }

      } catch (FileNotFoundException e1) {

          flag=false;

          e1.printStackTrace();

      }

      if(osw!=null){

        BufferedWriter bw=new BufferedWriter(osw);

         try {

          if(content!=null&&!"".equals(content)){

             bw.write(content);

             flag= true;

          }

         }catch (IOException e) {

          flag=false;

          e.printStackTrace();

        }finally{

          try {

             bw.close();

             osw.close();

          } catch (IOException e) {

             flag=false;

             e.printStackTrace();

          }         

        }

      }

      return flag;

   }

/** 

   *把内容写到文件或追加到文件中

   *@paramfilePathName文件名 

   *@paramList<String>文件内容 

   */   

  public static boolean writerFileIsAppend(String filePathName,String content){

       boolean flag=false;

       OutputStreamWriter osw=null;

       try {

           if (filePathName!=null&&!"".equals(filePathName)) {

              osw = new OutputStreamWriter(new FileOutputStream(filePathName,true));

           }

       } catch (Exception e1) {

           flag=false;

           e1.printStackTrace();

       }

       if(osw!=null){

         BufferedWriter bw=new BufferedWriter(osw);

         try {

           if(content!=null&&!"".equals(content)){

              bw.write(content);

              flag= true;

           }

         } catch (IOException e) {

           flag=false;

           e.printStackTrace();

         }finally{

           try {

              bw.close();

              osw.close();

           } catch (IOException e) {

              flag=false;

              e.printStackTrace();

                   

         }

       }

       return flag;

     }六、 RandomAccessFile

6.1:说明

  RandomAccessFile是一种特殊的文件流,可以用它在文件的任何地方查找或者插入数据

  RandomAccessFile同时实现了DataInputDataOutput接口,所以可以用它来读/写文件

  构造器:

---RandomAccessFile(java.io.File f,String mode)

---RandomAccessFile(String file,String mode)

6.2:代码示例

/**

    *Description: 读取文件最后一行内容

    *@param  fileName文件路径名+文件名

    */

   publicstatic String getfinalLineData(StringpathName){

      RandomAccessFileraf = null;

      StringlastLine = "";

      try {

          raf = new RandomAccessFile(pathName,"r");

          long len = raf.length();

          if (len != 0L) {

             long pos = len - 1;

             while (pos > 0) {

                 pos--;

                 raf.seek(pos);

                 if (raf.readByte() =='\n') {

                    lastLine = raf.readLine();

                    break;

                 }

             }

          }

      } catch (Exception e) {

          e.printStackTrace();

      }finally {

           if (raf !=null) {

               try {

                 raf.close();

               } catch (IOException e1) {

                 e1.getStackTrace();

               }

           }

       }

      return lastLine;

   }

七、 注意事项

1、将高级流“套接“在低级流上,这样起到缓冲的作用可以提高效率。

2、将使用完的流关闭,释放资源。

3、读取如图片、声音、影像等文件用字节流。

4、读取如文本等文件用字符流。

5、根据具体的数据格式选择合适的读写方法、如按行读写、按照字节读写等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值