java 流 写

package 流;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
public class 写 {
    
    public static String path="d://test.txt";
    public static void main(String[] args) {
        BufferedWriterDemo();
    }
    
    //字节流
    public static void OutputStream(){
        
        try {
            OutputStream outputStream=new FileOutputStream(path);
            String str="hello world";
            byte[] b=str.getBytes();
            for(int i=0; i<b.length; i++)
                try {
                    outputStream.write(b[i]);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //字符流
    public static void FileWriterDemo(){
        FileWriter fileWriter;
        try {
            fileWriter = new FileWriter(path);
            String str="hello word";
            fileWriter.write(str);
            fileWriter.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //缓冲流
    public static void BufferedWriterDemo(){
        BufferedWriter bufferedWriter;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            String str="hello word";
            bufferedWriter.write(str);
            bufferedWriter.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

 

posted @ 2014-10-01 23:58  凡尘里的一根葱  阅读(201)  评论(0编辑  收藏  举报