JAVA提高篇(21)--FileReader和FileWriter

一、FileWriter方法的使用:

[java]  view plain  copy
  1. public static void main(String[] args) {  
  2.         FileWriter fw =null;  
  3.         try {  
  4.             //创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件  
  5.             //而且该文件会被创建到指定目录下,如果该目录下已有同名文件,将被覆盖  
  6.             //其实该步就是在明确数据要存放的目的地  
  7.             fw= new FileWriter("Demo.txt",true); //这里传递一个true参数,代表不覆盖已有的文件,并在已有文件的莫维持进行数据续写  
  8.               
  9.             //调用writer方法,将字符串写到流中  
  10.             fw.write("人这一辈子不能太懦弱,这样的话就有人会瞧不起你的,碉堡你知道吗?你再这样下去会没女票的哦亲。。。");  
  11.             fw.write("\r\n出来爱死你了");  
  12.               
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }finally{  
  16.             if (fw!=null) {  
  17.                 try {  
  18.                     //关闭流资源,但是在关闭之前会刷新一次内容的缓存中数据,将数据刷新到目的地中  
  19.                     //和flush的区别:flush刷新后,流可以继续使用,而close刷新后,会将流关闭。  
  20.                     fw.close();  
  21.                 } catch (IOException e) {  
  22.                     e.printStackTrace();  
  23.                 }  
  24.             }  
  25.         }  
  26.     }  
二、FileReader方法的使用:
[java]  view plain  copy
  1. public static void main(String[] args) throws IOException{  
  2.         GetFileReader1();  
  3.         System.out.println("----------------");  
  4.         GetFileReader2();  
  5.     }  
  6.       
  7.     //读取文件内容的方式一  
  8.     public static void GetFileReader1() throws IOException{  
  9.         //创建一个文件读取流对象,和指定名称的文件相关联。  
  10.                 //要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException  
  11.                 FileReader fr = new FileReader("Demo.txt");  
  12.                   
  13.                 //调用读取流对象的read方法  
  14.                 //read()一次只能读取一个字符,而且会自动往下读  
  15. //              while (true) {  
  16. //                  int read = fr.read();  
  17. //                  if (read==-1) {  
  18. //                      break;  
  19. //                  }  
  20. //                  System.out.print((char)read);//在这里必须强转成char类型才能显示  
  21. //              }  
  22.                   
  23.                 int read = 0;  
  24.                 while ((read = fr.read())!=-1) {  
  25.                     System.out.print((char)read);  
  26.                 }  
  27.                   
  28.                 fr.close();  
  29.     }  
  30.       
  31.     /** 
  32.      * 第二种方式:通过字符数组进行读取 
  33.      */  
  34.     public static void GetFileReader2() throws IOException{  
  35.         FileReader fReader = new FileReader("Demo.txt");  
  36.         //定义一个字符数组,拥有存储读到的字符  
  37.         //读read(char[])返回的是读到的字符个数  
  38.         char arr [] = new char[1024];  
  39.         int num = 0;  
  40.         while ((num=fReader.read(arr)) !=-1) {  
  41.             System.out.print(new String(arr,0,num));  
  42.         }  
[java]  view plain  copy
  1. <span style="white-space:pre">        </span>fReader.close();  
  2.     }  
上面结果输出:

人这一辈子不能太懦弱,这样的话就有人会瞧不起你的,碉堡你知道吗?你再这样下去会没女票的哦亲。。。
出来爱死你了----------------
人这一辈子不能太懦弱,这样的话就有人会瞧不起你的,碉堡你知道吗?你再这样下去会没女票的哦亲。。。
出来爱死你了
三、练习:读取一个.java文件

[java]  view plain  copy
  1. //读取一个.java文件  
  2.     public static void Demo() throws IOException{  
  3.         FileReader fr= new FileReader("E:\\JAVA\\TestMap\\src\\com\\diaobao\\map\\TastMap.java");  
  4.         char [] render = new char[1024];  
  5.         int num= 0;  
  6.         while ((num = fr.read(render))!=-1) {  
  7.             System.out.print(new String(render,0,num));  
  8.         }  
  9.     }  




版权声明:本文为博主原创文章,未经博主允许不得转载。

java中的 FileWriter类 和 FileReader类的一些基本用法

1,FileWriter类(字符输出流类)

构造方法:FileWriter fw = new FileWriter(String fileName);//创建字符输出流类对象和已存在的文件相关联。文件不存在的话,并创建。

                                             如:FileWriter fw = new FileWriter("C:\\demo.txt");

                  FileWriter fw = new FileWriter(String fileName,boolean append);//创建字符输出流类对象和已存在的文件相关联,并设置该该流对文件的操作是否为续写。

                                             如:FileWriter fw = new FileWriter("C:\\demo.txt",ture); //表示在fw对文件再次写入时,会在该文件的结尾续写,并不会覆盖掉。

主要方法: void write(String str)   //写入字符串。当执行完此方法后,字符数据还并没有写入到目的文件中去。此时字符数据会保存在缓冲区中。

                                                        此时在使用刷新方法就可以使数据保存到目的文件中去。

                  viod flush()                //刷新该流中的缓冲。将缓冲区中的字符数据保存到目的文件中去。

                  viod close()               //关闭此流。在关闭前会先刷新此流的缓冲区。在关闭后,再写入或者刷新的话,会抛IOException异常。

[java]  view plain  copy
  1. package filewriter;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5.   
  6. public class Filewriter {  
  7.   
  8.     private static final String LINE_SEPARATOR = System.getProperty("line.separator");  
  9.   
  10.     /** 
  11.      *  
  12.      * @param args 
  13.      * @throws IOException  
  14.      */  
  15.     public static void main(String[] args) throws IOException {  
  16.         /** 
  17.          * 创建一个可以往文件中写入字符数据的字符流输出流对象 
  18.          * 创建时必须明确文件的目的地 
  19.          * 如果文件不存在,这回自动创建。如果文件存在,则会覆盖。 
  20.          * 当路径错误时会抛异常 
  21.          *  
  22.          * 当在创建时加入true参数,回实现对文件的续写。 
  23.          */  
  24.         FileWriter fw = new FileWriter("C:\\demo1.txt",false);  
  25.         /** 
  26.          * 调用该对象的write方法,向文件写入字符。 
  27.          *  
  28.          * 其实写入到了临时存储缓冲区中 
  29.          */  
  30. //      fw.write("hello \r\nworld!");//windows中的换行为\r\n    unix下为\r。  
  31.         fw.write("aello"+LINE_SEPARATOR+"world!");  
  32.         fw.write("hahaha");  
  33.         /** 
  34.          * 进行刷新,将字符写到目的地中。 
  35.          */  
  36. //      fw.flush();  
  37.         /** 
  38.          * 关闭流,关闭资源。在关闭前会调用flush方法 刷新缓冲区。关闭后在写的话,会抛IOException 
  39.          */  
  40.         fw.close();  
  41.           
  42.   
  43.     }  
  44.   
  45. }  

关于FileWriter的的异常处理。

[java]  view plain  copy
  1. package filewriter;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5.   
  6. public class IOExceptionDemo {  
  7.   
  8.     private static final String LINE_SEPARATOR = System.getProperty("line.separator");  
  9.     public static void main(String[] args) {  
  10.   
  11.         FileWriter fw = null;  
  12.         try {  
  13.             fw = new FileWriter("k:\\Demo.txt"true);  
  14.             fw.write("hello" + LINE_SEPARATOR + "world!");  
  15.         } catch (Exception e) {  
  16.             System.out.println(e.toString());  
  17.         } finally {  
  18.             if (fw != null)  
  19.                 try {  
  20.                     fw.close();  
  21.                 } catch (IOException e) {  
  22.                     throw new RuntimeException("关闭失败!");  
  23.                 }  
  24.         }  
  25.     }  
  26. }  

2,FileReader类

1,构造方法

FileReader fr = new FileReader(String fileName);//使用带有指定文件的String参数的构造方法。创建该输入流对象。并关联源文件。

2,主要方法

int read(); // 读取单个字符。返回作为整数读取的字符,如果已达到流末尾,则返回 -1。

int read(char []cbuf);//将字符读入数组。返回读取的字符数。如果已经到达尾部,则返回-1。

void close();//关闭此流对象。释放与之关联的所有资源。

[java]  view plain  copy
  1. package Filereader;  
  2.   
  3. import java.io.FileReader;  
  4. import java.io.IOException;  
  5.   
  6. public class FileReaderDemo {  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.         /** 
  10.          * 创建读取字符数据的流对象。 
  11.          * 读取路径不正确时会抛 IOException 
  12.          * 用以个读取流对象关联一个已存在文件。 
  13.          */  
  14.         FileReader fr = new FileReader("demo.txt");  
  15.         /** 
  16.          * 用Reader中的read方法读取字符。 
  17.          */  
  18.         /*int ch = fr.read(); 
  19.         System.out.print((char)ch); 
  20.         int ch1 = fr.read(); 
  21.         System.out.print((char)ch1); 
  22.         int ch2 = fr.read(); 
  23.         System.out.print((char)ch2);*/  
  24.         int ch = 0;  
  25.         while((ch = fr.read()) != -1){  
  26.             System.out.print((char)ch);  
  27.         }  
  28.         fr.close();  
  29.         }  
  30. }  


 

用FileReader  和 FileWriter 写的复制文本文件的小程序。

[java]  view plain  copy
  1. package IOtest;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7.   
  8. public class TxtCopy {  
  9.   
  10.     /** 
  11.      * 将C:\\的myHeart.txt copy 到 D:\\下 
  12.      *  
  13.      * 首先创建Reader读取数据数据的 读取流对象。 
  14.      *  
  15.      * @throws FileNotFoundException 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         FileReader fr = null;  
  19.         FileWriter fw = null;  
  20.         try {  
  21.             fr = new FileReader("C:\\my.txt");  
  22.             fw = new FileWriter("D:\\you.txt");  
  23.             //读一个字符,写一个字符方法  
  24. //          int ch = 0;  
  25. //  
  26. //          while ((ch = fr.read()) != -1) {  
  27. //              fw.write(ch);  
  28. //          }  
  29.             char []buf = new char[1024];  
  30.             int len = 0;  
  31.             //读一个数组大小,写一个数组大小方法。  
  32.             while((len = fr.read(buf)) != -1){  
  33.                 fw.write(buf, 0, len);                
  34.             }  
  35.               
  36.         } catch (Exception e) {  
  37.             System.out.println(e.toString());  
  38.         } finally {  
  39.             if (fr != null)  
  40.                 try {  
  41.                     fr.close();  
  42.                 } catch (Exception e2) {  
  43.                     throw new RuntimeException("关闭失败!");  
  44.                 }  
  45.             if (fw != null)  
  46.                 try {  
  47.                     fw.close();  
  48.                 } catch (IOException e) {  
  49.                     throw new RuntimeException("关闭失败!");  
  50.                 }  
  51.         }  
  52.     }  
  53. }  

举报
该文章已被禁止评论!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值