创建文件夹

根据日期创建文件夹:

先创建文件夹:

//1.使用递归方法-一层一层创建文件夹

public static void createFiles(File parentPath,String[] module,int currIndex) throws IOException{

if(parentPath.exists()){

String path=parentPath.getPath()+"/"+module[currIndex];

parentPath=new File(path);

if(!parentPath.exists()){parentPath.mkdir();}

if(currentIndex<module.length-1){

 createFiles(parentPath,module,++currIndex);

}

}

}

//2.使用递归-文件目录创建文件夹

public static void createFolder(String path){

File parentFile=new File(path);

if(!parentFile.getparentFile().exists()){

createFolder(parentFile.getparentFile().getPath());

parentFile.mkdir();

}

}

 

//main

public static void main(Stirng [] args)throws IOException(){

//1.

//File parentFile=new File(PropertiesUtil.getProperty("project.webroot.path"));

//String[] floders=new String[]{"1"."2","3","4"};

//createFiles(parentFile,floders,0);

//2.

String file="d:/1/2/3/4";

createFolder(file);

}

 

//service测试获取图片路径,Hex2byte类,使用2.

String jdljImage=PropertiesUtil.getProperty("project.webroot.path"));

Calender cal=Calender.getinstance();

int year=cal.get(Calender.YEAR);

int month=cal.get(calender.MONTH)+1;

int day=cal.get(calender.DAY_OF_MONTH);

String image=mo.getValue("image").toString();

String  datePath="/upload/image/bbsInfo/"+year+"/"+month+"/"+day+"/";

if(!"".equals(image) && !"\"\"".equals(image)){

 

String parentpath=jdljImage+datePath;

StringUtil.createFolder(parentpath);//调用文件

String fileName=datePath+new Date().getTime()+".jpg";

String filePath=jdljImage+fileName;

//测试用 image=Hex2byte.getImgeHexString(image,"jpg");

Hex2byte.saveImage(image,filePath."jpg");

params.put("image",fileName);将fileName保存在数据库中

}

 

 jdk中提供了创建目录的两种方法实现 mkdir() 和 mkdirs()

前者是在给定目录结构path参数下创建指定的目录,
如果path中少了一层目录没有创建则会抛出异常(FileNotFoundException)
而第二个方法,则是相对安全的实现,因为他会自动创建不存在的父级目录。

 

 

1、目录已经存在,创建文件夹
public static void main(String [] args){   

String filePath = "c:/1";   

File fp = new File(filePath);   

 // 目录已存在创建文件夹   

if (!fp.exists()) {   

fp.mkdir();// 目录不存在的情况下,会抛出异常   

 }   

 }  

2.目录不存在,创建整个目录

public static void main(String [] args){   

String filePath = "d:/1/2/3/4/5/6";   

File fp = new File(filePath);   

 // 创建目录   

 if (!fp.exists()) {   

  fp.mkdirs();// 目录不存在的情况下,创建目录。   

}

 

 

 

 

 

 (1)

  1. File file=new File("C:\\Users\\QPING\\Desktop\\JavaScript\\2.htm");    
  2. if(!file.exists())    
  3. {    
  4.     try {    
  5.         file.createNewFile();    
  6.     } catch (IOException e) {    
  7.         // TODO Auto-generated catch block     
  8.         e.printStackTrace();    
  9.     }    
  10. }   

(2)

  1. File file =new File("C:\\Users\\QPING\\Desktop\\JavaScript");    
  2. //如果文件夹不存在则创建     
  3. if  (!file .exists()  && !file .isDirectory())      
  4. {       
  5.     System.out.println("//不存在");  
  6.     file .mkdir();    
  7. else   
  8. {  
  9.     System.out.println("//目录存在");  

package com.xhkj.util;

import java.io.File;
import java.io.IOException;

public class CreateFileUtil {

public static boolean CreateFile(String destFileName) {
    File file = new File(destFileName);
    if (file.exists()) {
     System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
     return false;
    }
    if (destFileName.endsWith(File.separator)) {
     System.out.println("创建单个文件" + destFileName + "失败,目标不能是目录!");
     return false;
    }
    if (!file.getParentFile().exists()) {
     System.out.println("目标文件所在路径不存在,准备创建。。。");
     if (!file.getParentFile().mkdirs()) {
      System.out.println("创建目录文件所在的目录失败!");
      return false;
     }
    }

    // 创建目标文件
    try {
     if (file.createNewFile()) {
      System.out.println("创建单个文件" + destFileName + "成功!");
      return true;
     } else {
      System.out.println("创建单个文件" + destFileName + "失败!");
      return false;
     }
    } catch (IOException e) {
     e.printStackTrace();
     System.out.println("创建单个文件" + destFileName + "失败!");
     return false;
    }
}



public static boolean createDir(String destDirName) {
    File dir = new File(destDirName);
    if(dir.exists()) {
     System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
     return false;
    }
    if(!destDirName.endsWith(File.separator))
     destDirName = destDirName + File.separator;
    // 创建单个目录
    if(dir.mkdirs()) {
     System.out.println("创建目录" + destDirName + "成功!");
     return true;
    } else {
     System.out.println("创建目录" + destDirName + "成功!");
     return false;
    }
}



public static String createTempFile(String prefix, String suffix, String dirName) {
    File tempFile = null;
    try{
    if(dirName == null) {
     // 在默认文件夹下创建临时文件
     tempFile = File.createTempFile(prefix, suffix);
     return tempFile.getCanonicalPath();
    }
    else {
     File dir = new File(dirName);
     // 如果临时文件所在目录不存在,首先创建
     if(!dir.exists()) {
      if(!CreateFileUtil.createDir(dirName)){
       System.out.println("创建临时文件失败,不能创建临时文件所在目录!");
       return null;
      }
     }
     tempFile = File.createTempFile(prefix, suffix, dir);
     return tempFile.getCanonicalPath();
    }
    } catch(IOException e) {
     e.printStackTrace();
     System.out.println("创建临时文件失败" + e.getMessage());
     return null;
    }
}


public static void main(String[] args) {
    // 创建目录
    String dirName = "c:/test/test0/test1";
    CreateFileUtil.createDir(dirName);
    // 创建文件
    String fileName = dirName + "/test2/testFile.txt";
    CreateFileUtil.CreateFile(fileName);
    // 创建临时文件
    String prefix = "temp";
    String suffix = ".txt";
    for(int i = 0; i < 10; i++) {
     System.out.println("创建了临时文件:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));
    }
  
}

 //删除制定文件夹的所有文件及根文件夹

  public void deleteFile(String path) {

  // TODO Auto-generated method stub

  File f = new File(path);

  if(f.isDirectory())

  {

  File[] file = f.listFiles();

  for (File file2 : file) {

  this.deleteFile(file2.toString());

  file2.delete();

  }

  }else

  {

  f.delete();

  }

  f.delete();

parentFile.mkdir(); createFolder(file);   1、目录已经存在,创建文件夹 public static void main(String [] args){   

猜你喜欢

转载自starbhhc.iteye.com/blog/2103554