首页 > 编程语言 > 详细

JAVA用流在指定路径下生成文件

时间:2015-03-31 14:41:02      阅读:503      评论:0      收藏:0      [点我收藏+]

       // 待处理的流

       ByteArrayOutputStream bao = new ByteArrayOutputStream();


      // 定义文件根路径:TOMCAT的temp路径 + 时间戳

       String baseDir = System.getProperty("java.io.tmpdir")  + System.currentTimeMillis();


       //待输出文件流对象

       FileOutputStream fos = null;
       try {

             // 自定义文件路径

             String customDir = "dir1/dir2/dir3";

             File file = new File(dirName + baseDir + "/" + customDir + "file.txt");

             //如果路径(无论多少层路径)不存在
             if(!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                    file.createNewFile();
            }

            fos = new FileOutputStream(file);
            bao.writeTo(fos);
            fos.flush();
            fos.close();
            bao.flush();
            bao.close();
       } catch (IOException ioe) {
             ioe.printStackTrace();
      }

JAVA用流在指定路径下生成文件

原文:http://blog.csdn.net/zzq2yz1314/article/details/44779051

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