1. 程式人生 > >java寫入檔案的幾種方法

java寫入檔案的幾種方法


package com.yiibai.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileExample {
 public static void main(String[] args) {

  FileOutputStream fop = null;
  File file;
  String content = "This is the text content";

  try {

   file = new File("c:/newfile.txt");
   fop = new FileOutputStream(file);

   // if file doesnt exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }

   // get the content in bytes
   byte[] contentInBytes = content.getBytes();

   fop.write(contentInBytes);
   fop.flush();
   fop.close();

   System.out.println("Done");

  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (fop != null) {
     fop.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

//更新的JDK7例如,使用新的“嘗試資源關閉”的方法來輕鬆處理檔案。

package com.yiibai.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileExample {
 public static void main(String[] args) {

  File file = new File("c:/newfile.txt");
  String content = "This is the text content";

  try (FileOutputStream fop = new FileOutputStream(file)) {

   // if file doesn't exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }

   // get the content in bytes
   byte[] contentInBytes = content.getBytes();

   fop.write(contentInBytes);
   fop.flush();
   fop.close();

   System.out.println("Done");

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

相關推薦

java寫入檔案方法

  在java中我常用的向檔案中寫入新內容的三種方法,分別是FileWritter,BufferedWriter ,FileOutputStream下面我分別給各位介紹三個例項希望對你有所幫助。   一,FileWritter寫入檔案   FileWritter, 字元流寫

PHP檔案寫入方法

通過fwrite $file= fopen("test.txt","a+"); //次方法會自動生成檔案test,txt,a表示追加寫入, //w代表替換寫入 fwrite($file,"寫入程式碼

java讀取寫入檔案方式效率比較

public class ReadTxtJson {public static String readTxtFile(String FileName) throws Exception {BufferedInputStream bufferedInputStream = n

java中有方法可以實現一個執行緒?用什麼關鍵字修飾同步方法 stop()和suspend()方法為何不推薦使用?

java5以前,有兩種實現方法,分別使用new Thread()和new Thread(runnable)形式,第一種繼承Thread類,直接呼叫thread的run方法,所以,我們往往使用Thread子類,即new SubThread()。第二種是實現Runn

java中有方法可以實現一個執行緒?用什麼關鍵字修飾同步方法? stop()和suspend()方法為何不推薦使用?

java5以前,有如下兩種: 第一種: new Thread(){}.start();這表示呼叫Thread子類物件的run方法,new Thread(){}表示一個Thread的匿名子類的例項物件,子類加上run方法後的程式碼如下: new Thread(){ publi

java寫入檔案方法分享

原博:https://www.jb51.net/article/47062.htm 一,FileWritter寫入檔案 FileWritter, 字元流寫入字元到檔案。預設情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布林)值作為FileWritter建構函式的第二

java寫入檔案方法

package com.yiibai.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample {  public static v

java寫入文件的方法分享

java一、FileWritter寫入文件 FileWritter,字符流寫入字符到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個真(布爾)值作為FileWritter構造函數的第二個參數,它會保留現有的內容,並追加新內容在文件的末尾。 替換所有現有的內容與新的內容。

Java讀取檔案方法

public class ReadFromFile { /** * 以位元組為單位讀取檔案,常用於讀二進位制檔案,如圖片、聲音、影像等檔案。 */ public static void readFileByBytes(String fileName) { Fil

Java檔案路徑中獲取檔名的方法

Java從檔案路徑中獲取檔名的幾種方法 舉例:String fName =” G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif ” 方法一: ? View Code 

java中把檔案拷貝到指定目錄下最簡單方法

java中把檔案拷貝到指定目錄下最簡單幾種方法       String savePath =  "D:/file" ; // 檔案儲存到d盤的file目錄下 File savefile = 

Java中spring讀取配置檔案方法

    在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據筆者工作中用到的讀取配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。     一、讀取xml配置檔案     (一)新建一個java bean

Java壓縮解壓縮檔案方法

Java壓縮解壓縮檔案的方法有,第一中藉助java jdk自帶的ZipOutputStream和ZipInputStream。 第二種,藉助第三方jar, 例如Apache Commons Compress和Ant。 下面以Ant為例詳細介紹。 前提,需要將

java讀取配置檔案方法

在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據筆者工作中用到的讀取配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。 一.讀取xml配置檔案 (一)新建一個java bean(HelloBean.java

java 讀取配置檔案方法

      讀取.properties配置檔案在實際的開發中使用的很多,總結了一下,有以下幾種方法(僅僅是我知道的): 一.通過jdk提供的java.util.Properties類         此類繼承自java.util.HashTable,即實現了Map介面

python寫入csv檔案方法總結

最常用的一種方法,利用pandas包import pandas as pd #任意的多組列表 a = [1,2,3] b = [4,5,6] #字典中的key值即為csv中列名 dataframe = pd.DataFrame({'a_name':a,'b_nam

Java獲取檔案路徑的方法

第一種: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); 結果: C:\Documents%20and%20Settings\Administrato

Java讀取配置檔案方法以及路徑問題

在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據筆者工作中用到的讀取配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。 一.讀取xml配置檔案 (一)新建一個java bean(HelloBean.java) j

16、java寫入檔案方式

package com.tij.io.file;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputSt

java讀取.properties配置檔案方法

讀取.properties配置檔案在實際的開發中使用的很多,總結了一下,有以下幾種方法(僅僅是我知道的): 一.通過jdk提供的java.util.Properties類         此類繼承自java.util.HashTable,即實現了Map介面,所以,可使