您的位置:首页 > 编程语言 > Java开发

Java,IO之FileOutputStream和FileWriter写入文件(疯狂java讲义)

2012-07-14 14:28 369 查看
import java.io.*;  
    public class FileOutputStreamTest   
    {  
        public static void main(String[] args) throws IOException  
        {  
            FileInputStream fis = null;  
            FileOutputStream fos = null;  
            try  
            {  
                //创建字节输入流  
                fis = new FileInputStream("FileOutputStreamTest.java");  
                //创建字节输出流  
                fos = new FileOutputStream("newFile.txt");  
                byte[] bbuf = new byte[32];  
                int hasRead = 0;  
                //循环从输入流中取出数据  
                while((hasRead = fis.read(bbuf)) > 0)  
                {  
                    //每读取一次,即写入文件输出流,读了多少,就写多少  
                    fos.write(bbuf,0,hasRead);  
                }  
            }  
            catch (IOException ioe)  
            {  
                ioe.printStackTrace();  
            }  
            finally  
            {  
                if(fis != null)  
                {  
                    fis.close();  
                }  
                if(fos != null)  
                {  
                    fos.close();  
                }  
            }  
        }  
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: