gistfile1.txt package com.htht.util;import java.io.*;import java.nio.charset.Charset;import java.util.*;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;/** * Created by Administrator on 2017/3/22. */public class SFileUtil
package com.htht.util; import java.io.*; import java.nio.charset.Charset; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * Created by Administrator on 2017/3/22. */ public class SFileUtil { /**文件重命名 * @param path 文件目录 * @param oldname 原来的文件名 * @param newname 新文件名 */ public static String renameFile(String path,String oldname,String newname){ if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名 File oldfile=new File(path+oldname); File newfile=new File(path+newname); if(!oldfile.exists()){ return "重命名文件不存在";// } if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名 return newname+"已经存在!"; else{ oldfile.renameTo(newfile); return "文件重命名成功"; } }else{ return "新文件名和旧文件名相同..."; } } /** * 新建目录 * * @param folderPath String 如 c:/fqf * @return boolean */ public static void newFolder(String folderPath) { try { String filePath = folderPath; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } } /** * 新建文件 * * @param filePathAndName String 文件路径及名称 如c:/fqf.txt * @param fileContent String 文件内容 * @return boolean */ public static void newFile(String filePathAndName, String fileContent) { try { String filePath = filePathAndName; filePath = filePath.toString(); //取的路径及文件名 File myFilePath = new File(filePath); /**如果文件不存在就建一个新文件*/ if (!myFilePath.exists()) { myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); //用来写入字符文件的便捷类, 在给出 File 对象的情况下构造一个 FileWriter 对象 PrintWriter myFile = new PrintWriter(resultFile); //向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自动行刷新的新 PrintWriter。 String strContent = fileContent; myFile.println(strContent); resultFile.close(); } catch (Exception e) { System.out.println("新建文件操作出错"); e.printStackTrace(); } } /** * 删除文件 * * @param filePathAndName String 文件路径及名称 如c:/fqf.txt * @return boolean */ public static void delFile(String filePathAndName) { try { String filePath = filePathAndName; filePath = filePath.toString(); File myDelFile = new File(filePath); myDelFile.delete(); } catch (Exception e) { System.out.println("删除文件操作出错"); e.printStackTrace(); } } /** * 删除文件夹 * * @param folderPath String * @return boolean */ public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); File myFilePath = new 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 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]; while ((byteread = inStream.read(buffer)) != -1) { fs.write(buffer, 0, byteread); } 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); } // 拷贝文件 private static void copyFile2(String source, String dest) { try { File in = new File(source); File out = new File(dest); FileInputStream inFile = new FileInputStream(in); FileOutputStream outFile = new FileOutputStream(out); byte[] buffer = new byte[10240]; int i = 0; while ((i = inFile.read(buffer)) != -1) { outFile.write(buffer, 0, i); }//end while inFile.close(); outFile.close(); }//end try catch (Exception e) { }//end catch }//end copyFile public static String getFolderFileNums(String path) { File folder = new File(path); File[] list = folder.listFiles(); int fileCount = 0, folderCount = 0; long length = 0; for (File file : list) { if (file.isFile()) { fileCount++; length += file.length(); } else { folderCount++; } } return fileCount + "," + folderCount; } public static List decompress(String zippath, String decompresspath) { Listlist = new ArrayList (); try { ZipInputStream Zin = new ZipInputStream(new FileInputStream(zippath), Charset.forName("GBK"));//输入源zip路径 BufferedInputStream Bin = new BufferedInputStream(Zin); String Parent = decompresspath; //输出路径(文件夹目录) File Fout = null; ZipEntry entry; try { while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) { Fout = new File(Parent, entry.getName()); list.add(entry.getName()); if (!Fout.exists()) { (new File(Fout.getParent())).mkdirs(); } FileOutputStream out = new FileOutputStream(Fout); BufferedOutputStream Bout = new BufferedOutputStream(out); int b; while ((b = Bin.read()) != -1) { Bout.write(b); } Bout.close(); out.close(); System.out.println(Fout + "解压成功"); } Bin.close(); Zin.close(); return list; } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return list; } public static String getExtensionName(String filename) { if ((filename != null) && (filename.length() > 0) && filename.contains(".")) { int dot = filename.lastIndexOf('.'); if ((dot > -1) && (dot < (filename.length() - 1))) { return filename.substring(dot + 1); } } return ""; } public static long getTotalSizeOfFilesInDir(final File file) { if (file.isFile()) return file.length(); final File[] children = file.listFiles(); long total = 0; if (children != null) for (final File child : children) total += getTotalSizeOfFilesInDir(child); return total; } public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); //递归删除目录中的子目录下 if (children.length > 0) { for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } else { return dir.delete(); } } // 目录此时为空,可以删除 return dir.delete(); } //按照文件大小排序 public static void orderByLength(String fliePath) { List< File> files = Arrays.asList(new File(fliePath).listFiles()); Collections.sort(files, new Comparator< File>() { public int compare(File f1, File f2) { long diff = f1.length() - f2.length(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1; } public boolean equals(Object obj) { return true; } }); for (File f : files) { if(f.isDirectory()) continue; // System.out.println(f.getName()+":"+f.length()); } } //按照文件名称排序 public static void orderByName(String fliePath) { List files = Arrays.asList(new File(fliePath).listFiles()); Collections.sort(files, new Comparator< File>() { public int compare(File o1, File o2) { if (o1.isDirectory() && o2.isFile()) return -1; if (o1.isFile() && o2.isDirectory()) return 1; return o1.getName().compareTo(o2.getName()); } }); // for (File f : files) { // System.out.println(f.getName()); // } } //按日期排序 public static void orderByDate(String fliePath) { File file = new File(fliePath); File[] fs = file.listFiles(); Arrays.sort(fs,new Comparator< File>(){ public int compare(File f1, File f2) { long diff = f1.lastModified() - f2.lastModified(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1; } public boolean equals(Object obj) { return true; } }); for (int i = fs.length-1; i >-1; i--) { // System.out.println(fs[i].getName()); // System.out.println(new Date(fs[i].lastModified())); } } }