茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672813 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
MinamiChiaki_00016.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/4/5 14:57:46

Java FileWriter類

這個類繼承自的OutputStreamWriter類。這個類是用於寫入字符流。

這個類有幾個構造函數來創建所需的對象。

下面的語法創建一個給定的文件對象文件字符寫對象。



FileWriter(File file)

下面的語法創建一個給定的文件對象文件字符寫對象。



FileWriter(File file, boolean append)

以下語法創建了一個文件描述符關聯的文件字符寫的對象。



FileWriter(FileDescriptor fd)

下面的語法創建給定文件名的文件字符寫對象。



FileWriter(String fileName)

下面的語法創建一個文件字符寫對象給出一個布爾值,指示是否附加寫入數據的文件名。



FileWriter(String fileName, boolean append)

一旦有文件字符寫對象,再有就是使用helper方法的列表,它可以用來操作文件。

SN 方法及描述
1
public void write(int c) throws IOException
寫入單個字符。
2 public void write(char [] c, int offset, int len)
寫入字符數組的偏離,len長度開始的部分。
3 public void write(String s, int offset, int len)
寫一個字符串的一部分從偏移和len長度開始。

例子:


下面的例子是用來演示:



import java.io.*;
public class FileRead{
public static void main(String args[])throws IOException{
File file = new File("Hello1.txt");
// creates the file
file
.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer
.write("This
is
an
example
"
);
writer
.flush();
writer
.close();
//Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr
.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); //prints the characters one by one
fr
.close();
}
}

這將產生以下結果:



This
is
an
example



原文出處:Java FileWriter類 - Java基礎教程
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|