码迷,mamicode.com
首页 > 编程语言 > 详细

java 流 写

时间:2014-10-02 01:06:11      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   java   for   sp   div   

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

    }
}

 

java 流 写

标签:style   blog   color   io   ar   java   for   sp   div   

原文地址:http://www.cnblogs.com/huangcongcong/p/4003724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!