该文件工具类主要对文件及文件夹的批量删除、批量复制&批量修改等,还有通过PrintStream刷新初始化文件,获取转换文件最近修改时间等,详情如下
import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.PrintStream;@b@import java.util.Calendar;@b@@b@public class FileUtils {@b@ @b@ public static String extractFileName(String fileName){@b@ int pos = fileName.lastIndexOf(47);@b@ String result = fileName;@b@ if (pos > -1)@b@ result = fileName.substring(pos + 1);@b@@b@ pos = fileName.lastIndexOf(92);@b@ if (pos > -1)@b@ result = fileName.substring(pos + 1);@b@@b@ return result;@b@ }@b@ @b@@b@ public static String extractFileExt(String fileName){@b@ int pos = fileName.lastIndexOf(46);@b@ if (pos > -1)@b@ return fileName.substring(pos + 1);@b@@b@ return "";@b@ }@b@ @b@@b@ public static String getTempFile(String tempPath, String prefix, String suffix){@b@ try{@b@ if ((prefix == null) || ("".equals(prefix.trim())))@b@ prefix = "tmp";@b@@b@ if (prefix.trim().length() < 3) {@b@ char[] d = { '0' };@b@ prefix = prefix + @b@ String.copyValueOf(d, 0, 3 - prefix.trim().length());@b@ }@b@ File tempf = @b@ File.createTempFile(prefix, suffix, new File(tempPath));@b@ return tempf.getAbsolutePath();@b@ } catch (IOException e) {@b@ e.printStackTrace(); }@b@ return "";@b@ }@b@@b@ public static String getTempPath(String tempPath, String prefix, String suffix){@b@ try{@b@ if ((prefix == null) || ("".equals(prefix.trim())))@b@ prefix = "tmp";@b@@b@ if (suffix == null)@b@ suffix = "";@b@@b@ Long i = Long.valueOf(0L);@b@@b@ String p = tempPath + "/" + prefix + i.toString() + suffix;@b@ File f = new File(p);@b@ if (!(f.exists())) {@b@ f.mkdirs();@b@ return f.getName();@b@ }@b@ i = Long.valueOf(i.longValue() + 1L);@b@ }@b@ catch (Exception e) {@b@ e.printStackTrace(); }@b@ return "";@b@ }@b@@b@ public static String getParentFileName(File f){@b@ try@b@ {@b@ return f.getParentFile().getName(); } catch (Exception e) {@b@ }@b@ return "";@b@ }@b@@b@ public static boolean copyFile(String fromFile, String toFile)@b@ throws Exception {@b@ @b@ OutputStream os = null;@b@ InputStream is = null;@b@ try {@b@ int cnt;@b@ os = new FileOutputStream(toFile);@b@ is = new FileInputStream(fromFile);@b@ byte[] buf = new byte[4096];@b@ do {@b@ cnt = is.read(buf);@b@ if (cnt > -1)@b@ os.write(buf, 0, cnt);@b@ }@b@ while (cnt >= buf.length);@b@@b@ return true;@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ return false;@b@ } finally {@b@ os.close();@b@ is.close();@b@ }@b@ }@b@@b@ public static String getFileNameWithoutType(String fileName){@b@ int pos;@b@ try{@b@ pos = fileName.lastIndexOf(".");@b@ String fileNameWithoutType = fileName.substring(0, pos);@b@ return fileNameWithoutType; } catch (Exception e) {@b@ }@b@ return "";@b@ }@b@@b@ public static String getFileTypeByFileNameWithUpperCase(String fileName){@b@ int pos;@b@ try{@b@ pos = fileName.lastIndexOf(".");@b@ String fileType = fileName.substring(pos + 1);@b@ return fileType.toUpperCase(); @b@ } catch (Exception e) {@b@ }@b@ return "";@b@ }@b@@b@ public static String getFileTypeByFileName(String fileName)@b@ {@b@ int pos;@b@ try{@b@ pos = fileName.lastIndexOf(".");@b@ String fileType = fileName.substring(pos + 1);@b@ return fileType; @b@ } catch (Exception e) {@b@ }@b@ return "";@b@ }@b@@b@ public static boolean deleteFileOrDirectory(String fileOrDir){@b@ File file = new File(fileOrDir);@b@ if (file.isFile())@b@ return file.delete();@b@@b@ String[] fileNames = file.list();@b@ if (fileNames != null)@b@ for (String fileName : fileNames)@b@ {@b@ if (!(deleteFileOrDirectory(file.getAbsolutePath() + "/" + @b@ fileName)))@b@ return false;@b@ }@b@ return file.delete();@b@ }@b@@b@ public static void bakFileDir(String resdir, String todir){@b@ File rdir = new File(resdir);@b@ if (!(rdir.isDirectory()))@b@ return;@b@@b@ todir = todir + rdir.getName() + "/";@b@ File tdir = new File(todir);@b@ if (!(tdir.exists()))@b@ tdir.mkdirs();@b@@b@ File[] files = rdir.listFiles();@b@ if (files.length > 0) {@b@ String resfile = null;@b@ String tofile = null;@b@ for (File file : files)@b@ if (file.isFile()) {@b@ resfile = file.getAbsolutePath();@b@ tofile = todir + file.getName();@b@ try {@b@ copyFile(resfile, tofile);@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ } else if (file.isDirectory()) {@b@ bakFileDir(file.getPath(), todir);@b@ }@b@ }@b@ }@b@@b@ public static void createInitCfg(String loginName, String licence, String Services, String fileName){@b@ FileOutputStream fis = null;@b@ PrintStream ps = null;@b@ File expFile = new File(fileName);@b@ try {@b@ fis = new FileOutputStream(expFile);@b@ ps = new PrintStream(fis);@b@ ps.println("[WebService]");@b@ ps.println("url=http://" + Services + "service/MyServices");@b@ ps.println("userName=" + loginName);@b@ ps.println("password=" + licence);@b@ ps.flush();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@@b@ if (ps != null)@b@ ps.close();@b@@b@ if (fis == null) return;@b@ try {@b@ fis.close();@b@ } catch (IOException e3) {@b@ e3.printStackTrace();@b@ }@b@ }@b@ finally@b@ {@b@ if (ps != null)@b@ ps.close();@b@@b@ if (fis != null)@b@ try {@b@ fis.close();@b@ } catch (IOException e3) {@b@ e3.printStackTrace();@b@ }@b@ }@b@ }@b@@b@ public static void delFolder(String folderPath) {@b@ try@b@ {@b@ delAllFile(folderPath);@b@ String filePath = folderPath;@b@ filePath = filePath.toString();@b@ new File(filePath);@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static boolean delAllFile(String path) {@b@ boolean flag = false;@b@ File file = new File(path);@b@ if (!(file.exists()))@b@ return flag;@b@@b@ if (!(file.isDirectory()))@b@ return flag;@b@@b@ String[] tempList = file.list();@b@ File temp = null;@b@ for (int i = 0; i < tempList.length; ++i) {@b@ if (path.endsWith(File.separator))@b@ temp = new File(path + tempList[i]);@b@ else@b@ temp = new File(path + File.separator + tempList[i]);@b@@b@ if (temp.isFile())@b@ temp.delete();@b@@b@ if (temp.isDirectory()) {@b@ delAllFile(path + "/" + tempList[i]);@b@ flag = true;@b@ }@b@ }@b@ return flag;@b@ }@b@@b@ public static void createDirectory(String path) {@b@ if ((path != null) && (!("".equals(path)))) {@b@ File f = new File(path);@b@ if (!(f.exists()))@b@ f.mkdirs();@b@ }@b@ }@b@@b@ public static String lastModifyHms(String path){@b@ return lastModifyHms(new File(path));@b@ }@b@@b@ public static String lastModifyHms(File file){@b@ if ((file.exists()) && (file.canRead())) {@b@ long l = file.lastModified();@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTimeInMillis(l);@b@ return cal.get(1) + "-" + (cal.get(2) + 1) + "-" + cal.get(5) + @b@ " " + cal.get(11) + ":" + cal.get(12) + ":" + cal.get(13);@b@ }@b@ return null;@b@ }@b@@b@}