首页 > 其他 > 详细

写出文件到本地

时间:2017-08-12 16:55:28      阅读:261      评论:0      收藏:0      [点我收藏+]
File file = new File("C:/Users/dao/Desktop/file.html");  //指定要写到那个文件。
if (!file.exists()) {                //检测是否存在,不存在,新建文件
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);           //新建一个文件输出流对象。     
            fileOutputStream.write(content.getBytes());                              //以字节的方式写出。
            fileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

还可以把FileOutputStream 转换成 OutPutStreamWriter对象和BufferedWriter对象来输出。

    try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8");
            outputStreamWriter.writer(content);                                               //字符流输出 。
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);          //缓存字符流输出。
            bufferedWriter.write(content);
            
//            fileOutputStream.write(content.getBytes());
//            fileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

直接从InputStream输入流中去字节写出

int tempByte = -1;
while((tempByte=input.read())>0){
fileoutputStream.write(tempByte);
}

 

写出文件到本地

原文:http://www.cnblogs.com/halo-yang/p/7350615.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!