zoukankan      html  css  js  c++  java
  • java中的 FileWriter类 和 FileReader类的一些基本用法

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

    |--用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。要自己指定这些值,可以先在 FileOutputStream 上构造一个 OutputStreamWriter。

    文件是否可用或是否可以被创建取决于底层平台。特别是某些平台一次只允许一个 FileWriter(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。

    FileWriter 用于写入字符流。要写入原始字节流,请考虑使用 FileOutputStream

    构造方法: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异常。

     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 
    world!");//windows中的换行为
        unix下为
    。  
    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的的异常处理。

     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类

    |--用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。

    FileReader 用于读取字符流。要读取原始字节流,请考虑使用 FileInputStream

    1,构造方法

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

    2,主要方法

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

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

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

     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 写的复制文本文件的小程序。

     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 }  
  • 相关阅读:
    Linked List Cycle leetcode java (链表检测环)
    Remove Duplicates from Sorted List II leetcode java
    Remove Duplicates from Sorted List leetcode java
    Merge Two Sorted Lists leetcode java
    Swap Nodes in Pairs leetcode java
    Median of Two Sorted Array leetcode java
    阿里云最便宜的四种域名注册
    nohup和&后台运行,进程查看及终止
    ipv6转ipv4 NAT64与DNS64基本原理概述
    ros使用pppoe拨号获取ipv6,并且下发IPV6的dns到客户机win7
  • 原文地址:https://www.cnblogs.com/FrankLei/p/6251158.html
Copyright © 2011-2022 走看看