1. 程式人生 > >javaIO-字元流-寫操作 FileWriter

javaIO-字元流-寫操作 FileWriter

javaIO 除了操作位元組,還可以操作字元,從使用情況來看,字元流的使用比例較高些。
Writer類是一個抽象類,它同時實現了Appendable, Closeable, Flushable介面。用於字元流的寫操作。它支援緩衝流,緩衝流的大小看下面的程式碼。直接的子類是OutputStreamWriter,FileWriter繼承了OutputStreamWriter。

    /**
     * Size of writeBuffer, must be >= 1
     */
    private final int writeBufferSize = 1024;

先看FileWriter類,它繼承了OutputStreamWriter,而OutputStreamWriter繼承了Writer。
FileWriter的構造方法,根據檔案路徑構造,或者根據File構造。它們都支援一個boolean型別的引數表示在原檔案基礎上追加或者覆蓋,當引數值為true的時候表示追加。在方法的內部,實際是呼叫了它父類的構造方法。具體些操作也是繼承父類的方法。看下它的原始碼

package java.io;
/**
 * @see OutputStreamWriter
 * @see FileOutputStream
 *
 * @author      Mark Reinhold
 * @since       JDK1.1
 */

public class FileWriter extends OutputStreamWriter {

    /**
     * Constructs a FileWriter object given a file name.
     *
     * @param fileName  String The system-dependent filename.
     * @throws
IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */
public FileWriter(String fileName) throws IOException { super(new FileOutputStream(fileName)); } /** * Constructs a FileWriter object given a file name with a boolean * indicating whether or not to append the data written. * * @param
fileName String The system-dependent filename. * @param append boolean if <code>true</code>, then data will be written * to the end of the file rather than the beginning. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */
public FileWriter(String fileName, boolean append) throws IOException { super(new FileOutputStream(fileName, append)); } /** * Constructs a FileWriter object given a File object. * * @param file a File object to write to. * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason */ public FileWriter(File file) throws IOException { super(new FileOutputStream(file)); } /** * Constructs a FileWriter object given a File object. If the second * argument is <code>true</code>, then bytes will be written to the end * of the file rather than the beginning. * * @param file a File object to write to * @param append if <code>true</code>, then bytes will be written * to the end of the file rather than the beginning * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason * @since 1.4 */ public FileWriter(File file, boolean append) throws IOException { super(new FileOutputStream(file, append)); } /** * Constructs a FileWriter object associated with a file descriptor. * * @param fd FileDescriptor object to write to. */ public FileWriter(FileDescriptor fd) { super(new FileOutputStream(fd)); } }

下面使用FileWriter來實現一個最簡單的例子,在檔案中寫入幾句話。
實現思路:
新建一個檔案(該檔案不存在則新建);
例項化一個 FileWriter,是在原檔案尾部追加內容還是覆蓋,根據需要選擇建構函式;
執行FileWriter的寫操作;
關閉流。


        File file = new File("e://app/fw.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer out =null;
        String str ="Test write method in class named FileWriter !";
        String res ="result: success!";
        try {
            //會覆蓋原先的內容
            out = new FileWriter(file); 
            //會在原先的檔案尾部追加新的內容
            //out = new FileWriter(file,true);
            out.write(str);
            out.write("\r\n"); //換行
            out.write(res);
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

相關推薦

javaIO-字元-操作 FileWriter

javaIO 除了操作位元組,還可以操作字元,從使用情況來看,字元流的使用比例較高些。 Writer類是一個抽象類,它同時實現了Appendable, Closeable, Flushable介面。用於字元流的寫操作。它支援緩衝流,緩衝流的大小看下面的程式碼。

檔案字元FileReader和FileWriter

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; impor

java -io字元FileWrite操作演示

FileWriter字元輸出流演示:   /* * FiileWriter 字元流的操作 * FileWriter 的構造方法 可傳遞 File型別 還可以傳遞String型別 * * 方法 : * write(int c) 傳遞一個位元組 * write(char[] a ) 傳遞一個字

字元Writer、緩衝字元BufferedWriter、FileWriter常用方法

一、字元流:                       讀寫時都是以字元為單位進行的。              1:字元流的父類:                                輸出流:Writer                            

Java中以字元形式操作檔案中的編碼問題

參考《編碼解碼模型和實現》,以字元流形式操作檔案的時候,一定要指定正確的編碼方案,否則會出現亂碼等問題。以字元流形式操作檔案包括兩種情形:以字元流的形式讀取檔案內容,將字元流寫入檔案中。 一、以字元流的形式讀取檔案內容 現在有一個檔案a.txt,檔案內容為“你好,Java

位元組字元,位元組字元的使用哪個多? java 讀操作大檔案 BufferedReader和RandomAccessFile

一 首先我們要知道 在程式中所有的資料都是以流的方式進行傳輸或儲存的   而流有兩種 位元組流用來處理位元組或二進位制物件 字元流主要用來處理字元或字串,一個字元佔兩個位元組 而上一篇的java 讀寫操作大檔案 BufferedReader和RandomAccessFile Buf

分別使用(字元)和(位元組)對檔案進行讀操作

一.使用(字元流)對檔案進行讀寫操作/* * 使用字元流對檔案進行讀寫操作 */ import java.io.BufferedReader; import java.io.FileInputSt

java基礎之I/O(一)------------字元的檔案讀操作

一.流的概念: java的輸入輸出稱為流,流是一組有順序的集合。而流的本質則是資料傳輸 二.流的分類:                  1.根據處理的功能分為位元組流(InputStream,OutPutStrean)和字元流(Writer,Reader)  

java-IO操作——使用帶有緩衝的字元資料

使用BufferedReader和PrintWriter實現檔案拷貝 package Test; import java.io.BufferedReader; import java.io.Bu

java IO 位元組字元操作總結一之File類

這篇文章將介紹有關java IO輸入輸出流的知識。首先說說字元編碼的問題,比較常用的編碼有gbk,utf-8等。 1.gbk 編碼中文佔用2個位元組,英文佔用1個位元組。 2、utf-8編碼中文佔用3個位元組,英文佔用1個位元組。 Java是雙位元組編碼,utf-16be編碼。即char

java IO 位元組字元操作總結二之位元組

上一篇,主要介紹了檔案類File和RandomAccessFile類的用法。接下來,我覺得還是將IO流分為位元組流和字元流兩部分介紹比較好。這樣不至於搞混亂,同時也便於對比。這一篇主要介紹位元組流。 1、位元組流 首先上一張位元組流的家族圖譜。 位元組流主要分為兩部分:InputS

Java IO操作—位元組(OutputStream、InputStream)和字元(Writer、Reader)

流的概念 在程式中所有的資料都是以流的方式進行傳輸或儲存的,程式中需要資料的時候就用輸入流讀取資料,而當程式需要將一些資料儲存起來的時候,就要使用輸出流完成。 程式中的輸入輸出都是以流的形式儲存的,流中儲存的實際上全部是位元組檔案。 位元組流和字元流 在java.io包中操作檔

javaIO位元組字元

流的概念 程式中的輸入輸出都是以流的形式儲存的,流中儲存的實際都是位元組檔案 位元組流與字元流 內容操作就四個類:OutputStream、InputStream、Writer、Reader 操作流程 使用File類操作的時候一定要有路徑,請注意分隔符。 實際上四個操作類都是抽象

java day21 IO 字元 FileReader FileWriter

(一)“位元組”的定義 位元組(Byte)是一種計量單位,表示資料量多少,它是計算機資訊科技用於計量儲存容量的一種計量單位。 (二)“字元”的定義 字元是指計算機中使用的文字和符號,比如1、2、3、A、B、C、~!·#¥%……—*()——+、等等。 21.01 字元

Java——IO(三)字元的讀與拷貝、裝飾設計模式

1.字元流的學習(FileReader與FileWriter):字元流是可以直接讀寫字元的IO流(只讀或者只寫時使用字元流)          (1)字元流讀取字元就要先讀取到位元組資料,然後轉化為字元;如果要寫出字元,需要把字元轉化為位元組再寫出          (2)讀

使用IO檔案的一些騷操作

序言 當需要對檔案進行操作時,使用IO流是不能避免的操作;比如業務中需要儲存一些請求的響應結果中的一些內容。當所需處理的檔案過大時,如果頻繁的關閉檔案流,會造成很大的開銷,何時關閉?往往會造成比較大的困擾。那麼如何才能比較優雅的處理檔案呢? 使用案例 情景 儲存資料時,行與行之間使用回車符隔開;一行的

java位元組字元操作檔案,指定編碼寫入和讀取,遞迴建立上層目錄

java的IO流分兩種流 位元組流 InputStream OutputStream 字元流 Reader Writer 他們都是抽象類 具體實現 位元組流 FileInputStream FileOutputStream 字元流 FileReader FileWriter

IO的讀操作

Java IO流的分類: 除了按照流的方向可以把流劃分為輸入流和輸出流兩類,按照流讀寫資料的基本單位把流劃分為位元組流和字元流兩類以外,還可以按照流是否直接連線實際資料來源,例如檔案、網路、位元組陣列等,將流又可以劃分為實體流和裝飾流兩大類。          其中實體流指

黑馬程式設計師——Java基礎---IO字元、位元組、轉換操作規律)

三、字元編碼 字元編碼通過轉換流來完成。在兩個物件進行構造的時候,可以加入字符集(即編碼表),可傳入編碼表的有: 轉換流:InuputStreamReader和OutputStreamWriter。列印流:PrintStream和PrintWriter,只有輸出流。轉換流的編碼應用 可以將字元以指定編

IO:讀操作

1、讀操作 1.1 讀二進位制流 一、讀取檔案,通過二進位制流的方式讀入byte陣列 方法一: FileInputStream fs = new FileInputStream("src\\FileInput.java"); byte[] buf = new b