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();
        }

    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。