<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 使用`BufferedWriter`,`PrintWriter`,`FileWriter`附加到 java 中的文件 > 原文: [https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/](https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/) 在本教程中,我們將學習如何使用 Java 將內容附加到文件中。有兩種方法可以追加: 1)使用`FileWriter`和`BufferedWriter`:在這種方法中,我們將在一個或多個字符串中包含內容,我們將把這些字符串附加到文件中。該文件可以單獨使用`FileWriter`附加,但使用`BufferedWriter`可以在維護緩沖區時提高性能。 2)使用`PrintWriter`:這是將內容附加到文件的最佳方式之一。無論你使用`PrintWriter`對象寫什么都會附加到文件中。 ## 1)使用`FileWriter`和`BufferedWriter`將內容附加到`File` ```java import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; class AppendFileDemo { public static void main( String[] args ) { try{ String content = "This is my content which would be appended " + "at the end of the specified file"; //Specify the file name and path here File file =new File("C://myfile.txt"); /* This logic is to create the file if the * file is not already present */ if(!file.exists()){ file.createNewFile(); } //Here true is to append the content to file FileWriter fw = new FileWriter(file,true); //BufferedWriter writer give better performance BufferedWriter bw = new BufferedWriter(fw); bw.write(content); //Closing BufferedWriter Stream bw.close(); System.out.println("Data successfully appended at the end of file"); }catch(IOException ioe){ System.out.println("Exception occurred:"); ioe.printStackTrace(); } } } ``` 輸出: ```java Data successfully appended at the end of file ``` 讓我們說`myfile.txt`的內容是: ```java This is the already present content of my file ``` 運行上述程序后,內容將是: ```java This is the already present content of my fileThis is my content which would be appended at the end of the specified file ``` ## 2)使用`PrintWriter`將內容附加到`File` `PrintWriter`為您提供更大的靈活性。使用此功能,您可以輕松格式化要附加到`File`的內容。 ```java import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; class AppendFileDemo2 { public static void main( String[] args ) { try{ File file =new File("C://myfile.txt"); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); //This will add a new line to the file content pw.println(""); /* Below three statements would add three * mentioned Strings to the file in new lines. */ pw.println("This is first line"); pw.println("This is the second line"); pw.println("This is third line"); pw.close(); System.out.println("Data successfully appended at the end of file"); }catch(IOException ioe){ System.out.println("Exception occurred:"); ioe.printStackTrace(); } } } ``` 輸出: ```java Data successfully appended at the end of file ``` 讓我們說`myfile.txt`的內容是: ```java This is the already present content of my file ``` 運行上述程序后,內容將是: ```java This is the already present content of my file This is first line This is the second line This is third line ``` #### 參考: * [`FileWriter` - JavaDoc](https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html "FileWriter") * [`BufferedWriter` - JavaDoc](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html) * [`PrintWriter` - Javadoc](https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html "PrintWriter")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看