JAVA编程 利用FileOutputStream类向myfile.txt文件写入'0'~'9'和字符串“文件和输入输出流”内容

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class IO {
    public static void main(String[] args) throws IOException {
        String str = "0,1,2,3,4,5,6,7,8,9 文件和输入输出流";//要写入文件内容
        String file="D://myfile.txt";//文件地址(必须存在)
        Input(file, str);
        Output(file);
    }

    public static void Output(String file) throws IOException,
            FileNotFoundException {
        
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "GBK"));
        
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }
        
        in.close();
    }

    public static void Input(String file, String str) throws IOException,
            FileNotFoundException {

        PrintWriter w = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream(file), "gbk"));

        w.write(str);
        System.out.println("input");
        w.close();

    }
}

 

posted @ 2013-09-21 23:32  fenglie  阅读(1277)  评论(0)    收藏  举报
版权所有,转载声明