黑马程序员技术交流社区

标题: FileReader,FileWriter [打印本页]

作者: ifuzhen    时间: 2014-4-19 22:46
标题: FileReader,FileWriter
  1. import java.util.*;
  2. import java.io.*;

  3. class Test
  4. {
  5.         public static void main(String [] args)
  6.         {
  7.                 fileReader();
  8.         }
  9.        
  10.         public static void fileReader()
  11.         {
  12.                 FileReader fr =null;
  13.                
  14.                 try
  15.                 {
  16.                         fr=new FileReader("d:\\who.txt");
  17.                         int ch =0;
  18.                         /*while(true)
  19.                         {
  20.                                 ch=fr.read();
  21.                                 if (ch==-1)
  22.                                         break;
  23.                                 sop("ch=="+(char)ch);
  24.                         }*/

  25.                         while((ch=fr.read())!=-1)
  26.                         {
  27.                                 sop("charinwhile2:"+(char)ch);
  28.                         }
  29.                        
  30.                 }
  31.                 catch (IOException e)
  32.                 {
  33.                         sop("FileNotFoundException:"+e.toString());
  34.                 }
  35.                
  36.                 finally
  37.                 {
  38.                         try
  39.                         {
  40.                                 fr.close();
  41.                         }
  42.                         catch (IOException e)
  43.                         {
  44.                                 sop(e.toString());
  45.                         }

  46.                 }
  47.                
  48.                
  49.                
  50.         }
  51.         public static void filedemo()
  52.         {
  53.                 FileWriter fw=null;
  54.                
  55.                 try
  56.                 {
  57.                         fw =new FileWriter ("d:\\who.txt",true);
  58.                         fw.write("\r\n come on");       
  59.                        
  60.                 }
  61.                 catch (IOException e)
  62.                 {
  63.                         sop(e.toString());
  64.                 }
  65.                
  66.                
  67.                 finally
  68.                 {
  69.                         try
  70.                         {
  71.                                 if (!(fw==null))
  72.                                 {
  73.                                         //fw.write("ppp");
  74.                                         fw.close();
  75.                                 }       
  76.                         }
  77.                         catch (IOException e )
  78.                         {
  79.                                 sop("catch:"+e.toString());
  80.                         }
  81.                        
  82.                 }
  83.                        
  84.         }
  85. }
复制代码

作者: ifuzhen    时间: 2014-4-19 23:03
  1. public static void FileReaderDemo2()
  2.         {
  3.                 FileReader fr=null;
  4.                 try
  5.                 {
  6.                         fr =new FileReader("d:\\who.txt");

  7.                         char[] buf =new char[1024];
  8.                         int num =0;

  9.                         while ((num=fr.read(buf))!=-1)
  10.                         {
  11.                                 sop(new String(buf,0,num));
  12.                         }
  13.                 }
  14.                 catch (IOException e)
  15.                 {
  16.                         sop("catch:"+e.toString());
  17.                 }
  18.                 finally
  19.                 {
  20.                         try
  21.                         {
  22.                                 fr.close();
  23.                         }
  24.                         catch (IOException e)
  25.                         {
  26.                                 sop("finally catch:"+e.toString());
  27.                         }
  28.                 }
  29.                
  30.         }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2