黑马程序员-------java基础-IO(1)

来源:互联网 发布:java二次开发前景怎样 编辑:程序博客网 时间:2024/06/01 09:37
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------


io学习总结

IO流的常用基类:

        1)字节流的抽象基流:InputStreamOutputStream

        2)字符流的抽象基流:ReaderWriter

示例:

[java] view plaincopyprint?
  1. import java.io.*; 
  2.  
  3. class FileWriterDemo  
  4.     public staticvoid main(String[] args)  
  5.     { 
  6.         writerDate("abcde");//写入数据 
  7.         writerFrom("zheshi:\r\nshenma");//续写数据,并附换行操作 
  8.     } 
  9.  
  10.     //在硬盘上创建一个文件并写入指定数据 
  11.     public staticvoid writerDate(String s) 
  12.     { 
  13.         FileWriter fw=null
  14.         try 
  15.         { 
  16.             fw=new FileWriter("demo.txt");//创建文件 
  17. fw.write(s);//将数据写入流 
  18.         } 
  19.         catch (IOException e) 
  20.         { 
  21.             throw new RuntimeException("写入失败"); 
  22.         } 
  23.         finally 
  24.         { 
  25.             if(fw!=null
  26.                 try 
  27.                 { 
  28.                     fw.close();//将写入流的数据刷到指定文件内,并关闭流资源 
  29.                 } 
  30.                 catch (IOException e) 
  31.                 { 
  32.                 } 
  33.         } 
  34.     } 
  35.  
  36.     //对已有文件的数据续写指定数据 
  37.     public staticvoid writerFrom(String s) 
  38.     { 
  39.         FileWriter fw=null
  40.         try 
  41.         { 
  42.             fw=new FileWriter("demo.txt",true);//传递一个true参数,代表不覆盖已有的文件。 
  43.             fw.write(s); 
  44.         } 
  45.         catch (IOException e) 
  46.         { 
  47.             throw new RuntimeException("写入失败"); 
  48.         } 
  49.         finally 
  50.         { 
  51.             try 
  52.             { 
  53.                 if(fw!=null
  54.                     fw.close(); 
  55.             } 
  56.             catch (IOException e) 
  57.             { 
  58.             } 
  59.         } 
  60.     } 

 

 

0 0
原创粉丝点击