1. 程式人生 > >多種方式實現寫檔案

多種方式實現寫檔案

/*
 * Copyright 2014 Chencp.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chencp.demo.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * WriteToFile.java
 * 作者: changping chen
 * 聯絡方式: 
[email protected]
* 描述:多種方式寫檔案 * */ public class WriteToFile { /** * 以位元組為單位寫檔案。適合於寫二進位制檔案,如圖片等 * @param fileName 檔名 */ public static void writeFileByBytes(String fileName){ File file = new File(fileName); OutputStream out = null; try { //開啟文輸出流 out = new FileOutputStream(file); String content = "檔案內容:\n1,The first line;\n2,The second line."; byte[] bytes = content.getBytes(); //寫入檔案 out.write(bytes); System.out.println("寫檔案【" + file.getAbsolutePath() + "】成功"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("寫檔案【" + file.getAbsolutePath() + "】失敗"); e.printStackTrace(); } finally { if (out != null) { try { out.close(); //關閉輸出檔案流 } catch (IOException e) { // TODO Auto-generated catch block System.out.println("關閉輸出檔案流失敗"); e.printStackTrace(); } } } } /** * 以字元為單位寫檔案 * @param fileName 檔名 */ public static void writeFileByChars(String fileName){ File file = new File(fileName); Writer writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(file)); String content = "檔案內容:\n1,The first line;\n2,The second line."; writer.write(content); System.out.println("寫檔案【" + file.getAbsolutePath() + "】成功"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("寫檔案【" + file.getAbsolutePath() + "】失敗"); e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); //關閉輸出檔案流 } catch (IOException e) { // TODO Auto-generated catch block System.out.println("關閉輸出檔案流失敗"); e.printStackTrace(); } } } } /** * 以行為單位寫檔案 * @param fileName 檔名 */ public static void writeFileByLines(String fileName){ File file = new File(fileName); PrintWriter writer = null; try { writer = new PrintWriter(new FileOutputStream(file)); //寫入各種型別資料 writer.println("檔案內容:"); writer.print(true); writer.print(123); writer.println(); writer.flush();//寫入檔案 System.out.println("寫檔案【" + file.getAbsolutePath() + "】成功"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("寫檔案【" + file.getAbsolutePath() + "】失敗"); e.printStackTrace(); } finally { if (writer != null) { writer.close(); //關閉輸出檔案流 } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String fileName = null; WriteToFile test = new WriteToFile(); fileName = "d:/temp/tempfile0.txt"; test.writeFileByBytes(fileName); fileName = "d:/temp/tempfile1.txt"; test.writeFileByChars(fileName); fileName = "d:/temp/tempfile2.txt"; test.writeFileByLines(fileName); } }