创建文件夹实例

来源:互联网 发布:淘宝晾衣架 编辑:程序博客网 时间:2024/05/17 01:24
//创建路径名
    String path = new SimpleDateFormat("yyyyMMdd").format(new Date());
    //String dir="/home/jqgj/srvmgr/servers/gmceip3.7/logs/phoneLogs/"+path;
    String dir="D:/home/jqgj/srvmgr/servers/gmceip3.7/logs/phoneLogs/"+path;
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    //创建文件名
    String tpmc=dir+"/"+context.getLogin().getUser().getName()+"_"+format.format(new Date())+".txt";
    byte[] b =clientLog.getLogData();
     File ret = null;  
           BufferedOutputStream stream = null;  
           try {
            //创建文件
            ret=new File(dir);
       if(!ret.exists()){
        ret.mkdirs();
       }
       //创建文件
       File filename = new File(tpmc);
       if (!filename.exists()) {
       filename.createNewFile();
       }
               FileOutputStream fstream = new FileOutputStream(filename);
               stream = new BufferedOutputStream(fstream);  
               stream.write(b);  
           } catch (Exception e) {  
               e.printStackTrace();  
           } finally {  
               if (stream != null) {  
                   try {  
                       stream.close();  
                   } catch (IOException e) {  
                       e.printStackTrace();  
                   }  
               }  
           }  
0 0