`

FileUtil (文件工具类),文件上传封装

阅读更多
public class FileUtil {


/**
* 功能:创建文件目录
* @param fileName
* @return
*/
public static boolean creatFold(String fileName){
File f  = new File(fileName);
if(!f.exists()){
return f.mkdirs();
}
return true;
}


/**
* 功能:数据寫入硬盤
* @param fold
*        地址
* @param fileName
*   文件名
* @param date
*   写入的数据
* @throws Exception
*/
public static void saveFile(String fold,String fileName,byte[] date) throws Exception{
FileOutputStream out =null;
BufferedOutputStream bStream = null;
ByteArrayInputStream in = new ByteArrayInputStream(date);

File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}
out = new FileOutputStream(new File(f,fileName));
bStream = new BufferedOutputStream(out);
byte[] bs = new byte[1024*10];
while(( in.read(bs)) != -1){
out.write(bs);
}
out.flush();
out.close();
in.close();
bStream.close();
}


/**
* 功能:文件写入硬盘
* @param fold
* @param fileName
* @param file
* @throws Exception
*/
public static void saveFile(String fold,String fileName,File file) throws Exception{
FileOutputStream out = null;
FileInputStream in = new FileInputStream(file);
File f = new File(fold);
if(!f.mkdir()){
if(!f.mkdirs()){
throw new Exception("创建文件路径失败");
}
}

out = new FileOutputStream(fold+fileName);
byte[] buffer = new byte[1024];
int len =0;
while((len = in.read(buffer))!= -1){
out.write(buffer, 0, len);
}

out.flush();
out.close();
in.close();
}


/**
* 功能:根据路径获取文件
* @param fold
* @return
*/
public static File getFile(String fold){
if(fold != null && !fold.equals("")){
File f = new File(fold);
if(f.isFile()){
return f;
}else{
return null;
}
}else{
return null;
}
}


/**
* 功能:取得文件夹
* @param fold
* @return
*/
public static File getFold(String fold){
if(fold !=null && !fold.equals("")){
File f = new File(fold);
if(f.isDirectory()){
return f;
}else{
return null;
}
}else{
return null;
}
}


/**
* 功能:删除文件
* @param filePath
*/
public static void delFile(String filePath){
if(filePath != null && !filePath.equals("")){
File f = new File(filePath);
if(f.isFile()){
f.delete();
}
}
}

/**   
     * 功能:删除文件夹   
     * @param filePathAndName String 文件夹路径及名称 如c:/fqf   
     * @param fileContent String   
     * @return boolean   
     */    
   public static void delFolder(String folderPath) {    
       try {    
           delAllFile(folderPath); //删除完里面所有内容    
           String filePath = folderPath;    
           filePath = filePath.toString();    
           java.io.File myFilePath = new java.io.File(filePath);    
           myFilePath.delete(); //删除空文件夹    
  
       }    
       catch (Exception e) {    
           System.out.println("删除文件夹操作出错");    
           e.printStackTrace();    
  
       }    
  
   } 


/**   
     * 功能:删除文件夹里面的所有文件   
     * @param path String 文件夹路径 如 c:/fqf   
     */    
   public static void delAllFile(String path) {    
       File file = new File(path);    
       if (!file.exists()) {    
           return;    
       }    
       if (!file.isDirectory()) {    
           return;    
       }    
       String[] tempList = file.list();    
       File temp = null;    
       for (int i = 0; i < tempList.length; i++) {    
           if (path.endsWith(File.separator)) {    
               temp = new File(path + tempList[i]);    
           }    
           else {    
               temp = new File(path + File.separator + tempList[i]);    
           }    
           if (temp.isFile()) {    
               temp.delete();    
           }    
           if (temp.isDirectory()) {    
               delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件    
               delFolder(path+"/"+ tempList[i]);//再删除空文件夹    
           }    
       }    
   }    
  
   /**   
     * 功能:复制单个文件   
     * @param oldPath String 原文件路径 如:c:/fqf.txt   
     * @param newPath String 复制后路径 如:f:/fqf.txt   
     * @return boolean   
     */    
   public static void copyFile(String oldPath, String newPath) {    
       try {    
           int bytesum = 0;    
           int byteread = 0;    
           File oldfile = new File(oldPath);    
           if (oldfile.exists()) { //文件存在时    
               InputStream inStream = new FileInputStream(oldPath); //读入原文件    
               FileOutputStream fs = new FileOutputStream(newPath);    
               byte[] buffer = new byte[1444];    
               int length;    
               while ( (byteread = inStream.read(buffer)) != -1) {    
                   bytesum += byteread; //字节数 文件大小    
                   System.out.println(bytesum);    
                   fs.write(buffer, 0, byteread);    
               }
               fs.flush();
               fs.close();
               inStream.close();    
           }    
       }    
       catch (Exception e) {    
           System.out.println("复制单个文件操作出错");    
           e.printStackTrace();    
  
       }    
  
   }    
  
   /**   
     * 复制整个文件夹内容   
     * @param oldPath String 原文件路径 如:c:/fqf   
     * @param newPath String 复制后路径 如:f:/fqf/ff   
     * @return boolean   
     */    
   public static void copyFolder(String oldPath, String newPath) {    
  
       try {    
           (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹    
           File a=new File(oldPath);    
           String[] file=a.list();    
           File temp=null;    
           for (int i = 0; i < file.length; i++) {    
               if(oldPath.endsWith(File.separator)){    
                   temp=new File(oldPath+file[i]);    
               }    
               else{    
                   temp=new File(oldPath+File.separator+file[i]);    
               }    
  
               if(temp.isFile()){    
                   FileInputStream input = new FileInputStream(temp);    
                   FileOutputStream output = new FileOutputStream(newPath + "/" +    
                           (temp.getName()).toString());    
                   byte[] b = new byte[1024 * 5];    
                   int len;    
                   while ( (len = input.read(b)) != -1) {    
                       output.write(b, 0, len);    
                   }    
                   output.flush();    
                   output.close();    
                   input.close();    
               }    
               if(temp.isDirectory()){//如果是子文件夹    
                   copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);    
               }    
           }    
       }    
       catch (Exception e) {    
           System.out.println("复制整个文件夹内容操作出错");    
           e.printStackTrace();    
  
       }    
  
   }    
  
   /**   
     * 移动文件到指定目录   
     * @param oldPath String 如:c:/fqf.txt   
     * @param newPath String 如:d:/fqf.txt   
     */    
   public static void moveFile(String oldPath, String newPath) {    
       copyFile(oldPath, newPath);    
       delFile(oldPath);    
  
   }    
  
   /**   
     * 移动文件到指定目录   
     * @param oldPath String 如:c:/fqf.txt   
     * @param newPath String 如:d:/fqf.txt   
     */    
   public static void moveFolder(String oldPath, String newPath) {    
       copyFolder(oldPath, newPath);    
       delFolder(oldPath);    
  
   }    
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics