1. 程式人生 > >java使用IO列印流輸出到檔案

java使用IO列印流輸出到檔案

列印流(只有輸出流、沒有輸入流)

列印流小例子

package com.uwo9.test01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class Test08 {

	public static void main(String[] args) {
		File toFile = new File("E:/Temp/Test1.txt");
		PrintStream ps = null;
		OutputStream os = null;
		try {
			// ps = new PrintStream(toFile);//可直接傳File
			os = new FileOutputStream(toFile, true);//true在原檔案上追加
			ps = new PrintStream(os, true);//true自動重新整理

			ps.println(10);
			ps.println("HelloWorld");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

	}

}