java中的FileWriter用於建立文件,可以寫入字元到建立的文件中;輸出流類別是FileWriter 類別的基類,因為它是從FileWriter 類別繼承的,並且該類別的建構子所做的假設是,如果要由我們指定這些值,則允許預設字元編碼和預設位元組緩衝區大小。輸出流編寫器必須在檔案輸出流上建構。
Java FileWriter 類別聲明:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Java.lang.Object Java.io.Writer Java.io.OutputStreamWriter Java.io.FileWriter
Java 中 FileWriter 的建構子
java中的FileWriter類別由多個建構子組成。他們是:
- FileWriter(File file):當給定一個檔案物件時,使用此方法 FileWriter(File file) 建構一個 FileWriter 物件。
- FileWriter(File file, Boolean append): 當給定一個檔案物件時,會建構一個FileWriter 對象,布林值指示是否使用該方法追加正在寫入的資料FileWriter(檔案文件,布爾追加)。
- FileWriter(FileDescriptor fd): 建構一個 FileWriter 對象,該對象與 FileWriter(FileDescriptor fd) 方法中指定為參數的文件描述符關聯。
- FileWriter(String fileName): 當給予檔案名稱時,使用此方法 FileWriter(String fileName) 建立一個 FileWriter 物件。
- FileWriter(String filename, Boolean append): 當給定檔案名稱時,會建立一個FileWriter 對象,Boolean 值指示是否使用此方法追加正在寫入的資料FileWriter(字串檔案名稱,布爾追加).
Java 中 FileWriter 的方法
java中的FileWriter類別由幾個方法組成,分別是:
- Public void write(int c) throws IOException: 使用此方法寫入單一字元; public void write(int c) 拋出 IOException。
- Public void write(char [] Stir) throws IOException: 使用此方法寫入字元陣列。 public void write(char [] Stir) 拋出 IOException。
- Public void write(string str) throws IOException: 使用此方法寫入字串。 public void write(string str) 拋出 IOException。
- Public void write(string str, int off, int len) throws IOException: 使用此方法寫入字符串的一部分Public void write(string str, int off, int len) throws IOException其中off 表示必須開始寫入字元的偏移量,len 表示要寫入的字元數。
- Public voidlush() throws IOException: 使用此方法刷新串流 Public voidlush() throws IOException.
- Public void close() throws IOException: 先刷新流,然後使用此方法關閉 writer Public void close() throws IOException。
用 Java 實作 FileWriter 的範例
以下是用Java實作FileWriter的範例:
範例#1
示範 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(); } }
輸出:
範例#2
使用 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(); } } }
範例 #3
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); } } }
輸出:
範例#4
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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500344564251.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的檔案寫入器" ></p> <h3 id="結論">結論</h3> <p>在本教程中,我們了解了FileWriter 的概念,如FileWriter 的定義、如何聲明FileWriter、FileWriter 中的構造函數以及編程示例來演示FileWriter 類的創建,使用FileWriter 類創建文本文件,使用FileWriter 類將文字追加到由內容組成的現有文件中,使用FileWriter 類別逐行寫入新文件,即一行一行地新增內容。 </p> <h3 id="推薦文章">推薦文章</h3> <p>這是 Java 中 FileWriter 的指南。這裡我們討論如何宣告 FileWriter、FileWriter 中的建構子及其方法以及程式碼實作。您也可以瀏覽我們其他推薦的文章以了解更多資訊 –</p> <ol> <li>Java 中的版面配置</li> <li>Java 編譯器</li> <li>Java 中的合併排序</li> <li>Java 並行流</li> </ol></string></string></string>
以上是Java 中的檔案寫入器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java實現“一次編寫,到處運行”通過編譯成字節碼並在Java虛擬機(JVM)上運行。 1)編寫Java代碼並編譯成字節碼。 2)字節碼在任何安裝了JVM的平台上運行。 3)使用Java原生接口(JNI)處理平台特定功能。儘管存在挑戰,如JVM一致性和平台特定庫的使用,但WORA大大提高了開發效率和部署靈活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具