java中的FileWriter用於建立文件,可以寫入字元到建立的文件中;輸出流類別是FileWriter 類別的基類,因為它是從FileWriter 類別繼承的,並且該類別的建構子所做的假設是,如果要由我們指定這些值,則允許預設字元編碼和預設位元組緩衝區大小。輸出流編寫器必須在檔案輸出流上建構。
Java FileWriter 類別聲明:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Java.lang.Object Java.io.Writer Java.io.OutputStreamWriter Java.io.FileWriter
java中的FileWriter類別由多個建構子組成。他們是:
java中的FileWriter類別由幾個方法組成,分別是:
以下是用Java實作FileWriter的範例:
示範 FileWriter 類別所建立的 Java 程式。
代碼:
import java.io.*; public class Read { public static void main(String args[])throws IOException { File file1 = new File("check.txt"); // A file is created file1.createNewFile(); // Object of FileWriter is created FileWriter writer1 = new FileWriter(file1); // Contents are written to the file writer1.write("Welcome to FileWriter"); writer1.flush(); writer1.close(); // Object of filereader is created FileReader read = new FileReader(file1); char [] a1 = new char[50]; read.read(a1); // array contents are read for(char ch : a1) System.out.print(ch); // characters are printed one by one read.close(); } }
輸出:
使用 FileWriter 類別建立文字檔案的 Java 程式。
代碼:
import java.io.*; public class Example { public static void main(String[] args) { //File constructor is initialized File file1 = new File("C:/Users/shivakumarsh/Desktop/Learning/source.txt"); try { boolean create = file1.createNewFile(); if (create) { System.out.println("File creation is succesful."); }else { System.out.println("There is already a file by this name."); } } catch (IOException e) { e.printStackTrace(); } } }
Java 程式示範將字串附加到文件末尾。
代碼:
import java.io.*; public class Example { public static void append(String fileName, String string) { try { // The file is opened in append mode BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName, true)); out1.write(string); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } } public static void main(String[] args) throws Exception { // A sample file is created with some text containing in it String fileName = "Shobha.txt"; try { BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName)); out1.write("Learning appending\n"); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } // The newly created file is appended with the string value passed here String string = "Learning is good"; append(fileName, string); // contents of the modified file are printed here try { BufferedReader in1 = new BufferedReader( new FileReader("Shobha.txt")); String mystr; while ((mystr = in1.readLine()) != null) { System.out.println(mystr); } } catch (IOException e) { System.out.println("Occurance of exception" + e); } } }
輸出:
Java 程式使用 FileWriter 逐行寫入檔案。
代碼:
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class List { public static void main(String[] args) throws Exception { ArrayList<String> list1 = new ArrayList<String>(); // The newly created file is appended with the string value passed here list1.add("Understanding an example in java"); list1.add("second line of the file"); list1.add("third line of the file"); writeFile("C:/Users/shivakumarsh/Desktop/Learning/source.txt", list1); System.out.println("file creation is successful"); } public static void write(String fileName, List<String> list1) throws Exception { FileWriter fwd = null; BufferedWriter bwd = null; try { fwd = new BufferedWriter(fwd); for (int j = 0; list1 != null && j < list.size(); j++) { bwd.write(list.get(j)); bwd.write("\n"); } } catch (Exception e1) { System.out.println("error occured:" + e1.getMessage()); throw e1; } finally { try { bwd.close(); } catch (Exception e1) { } try { fwd.close(); } catch (Exception e1) { } } } }
輸出:
在本教程中,我們了解了FileWriter 的概念,如FileWriter 的定義、如何聲明FileWriter、FileWriter 中的構造函數以及編程示例來演示FileWriter 類的創建,使用FileWriter 類創建文本文件,使用FileWriter 類將文字追加到由內容組成的現有文件中,使用FileWriter 類別逐行寫入新文件,即一行一行地新增內容。
這是 Java 中 FileWriter 的指南。這裡我們討論如何宣告 FileWriter、FileWriter 中的建構子及其方法以及程式碼實作。您也可以瀏覽我們其他推薦的文章以了解更多資訊 –
以上是Java 中的檔案寫入器的詳細內容。更多資訊請關注PHP中文網其他相關文章!