<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 功能強大 支持多語言、二開方便! 廣告
                # 如何在 java 中使用`FileOutputStream`寫入文件 > 原文: [https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/](https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/) 之前我們看過如何[在 Java](https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/) 中創建一個文件。在本教程中,我們將看到如何使用`FileOutputStream`在 java 中寫入文件。我們將使用`FileOutputStream`的[`write()`方法](https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#write(byte[]))將內容寫入指定的文件。這是`write()`方法的簽名。 ```java public void write(byte[] b) throws IOException ``` 它將指定字節數組中的`b.length`字節寫入此文件輸出流。如您所見,此方法需要字節數組才能將它們寫入文件。因此,在將內容寫入文件之前,我們需要將內容轉換為字節數組。 ## 完整代碼:寫入文件 在下面的例子中,我們將`String`寫入文件。要將`String`轉換為字節數組,我們使用[`String`類](https://beginnersbook.com/2013/12/java-strings/)的[`getBytes()`方法](https://beginnersbook.com/2013/12/java-string-getbytes-method-example/)。 ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileDemo { public static void main(String[] args) { FileOutputStream fos = null; File file; String mycontent = "This is my Data which needs" + " to be written into the file"; try { //Specify the file path here file = new File("C:/myfile.txt"); fos = new FileOutputStream(file); /* This logic will check whether the file * exists or not. If the file is not found * at the specified location it would create * a new file*/ if (!file.exists()) { file.createNewFile(); } /*String content cannot be directly written into * a file. It needs to be converted into bytes */ byte[] bytesArray = mycontent.getBytes(); fos.write(bytesArray); fos.flush(); System.out.println("File Written Successfully"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException ioe) { System.out.println("Error in closing the Stream"); } } } } ``` 輸出: ```java File Written Successfully ``` #### 參考: [`FileOutputStream` - JavaDoc](https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html)
                  <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>

                              哎呀哎呀视频在线观看