import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.zip.GZIPOutputStream; /** * 此类对文件或文件夹进行 增删改查等操作 * @author user */ public class FileUtil { public static final int BUFFER_SIZE = 1024 * 1000; private FileUtil() { } /** * * copy file * @param srcFileName * @param destFileDirName * @param destFileName */ public static void copyFile(String srcFileName, String destFileDirName, String destFileName) throws IOException { File srcFile = new File(srcFileName); File destFileDir = new File(destFileDirName); File destFile = new File(destFileDirName + destFileName); FileOutputStream fos = null; FileInputStream fis = null; //if the source file is empty. if (0 == srcFile.length()) { } //if target directory dosen't exist, make it. if (!destFileDir.exists()) { if (!destFileDir.mkdirs()) { } } try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); byte[] buffer = new byte[BUFFER_SIZE]; int i = 0; //copy content from source file to target file. while ((i = fis.read(buffer)) != -1) { fos.write(buffer, 0, i); fos.flush(); } } catch (FileNotFoundException e) { ; } finally { IOUtil.close(fis); IOUtil.close(fos); } } /** * * move file. * @param srcFileName * @param destFileName * @return */ public static boolean renameFile(String srcFileName, String destDir, String destFileName) { File srcFile = new File(srcFileName); try { makeDirectory(destDir); return srcFile.renameTo(new File(destDir + destFileName)); } catch (Exception e) { return false; } } /** * * delete file. * @param targetFileName * @return */ public static boolean deleteFile(String targetFileName) { File targetFile = new File(targetFileName); return deleteFile(targetFile); } /** * * Delete file. * @param file * @return */ public static boolean deleteFile(File file) { if (file.exists()) { if (file.delete()) { return true; } } return false; } /** * * get directory of the file. * @param fileName * @return */ public static String getFilePath(String fileName) { File targetFile = new File(fileName); if (targetFile.isFile() && targetFile.exists()) { return targetFile.getPath(); } else { return null; } } /** * * compress file. * @param srcFileName * @param destFileName * @throws IOException */ public static void zipFile(String srcFileName, String destDir, String destFileName) { InputStream fileIn = null; OutputStream fileOut = null; GZIPOutputStream gzip = null; try { makeDirectory(destDir); fileIn = new FileInputStream(srcFileName); ByteArrayOutputStream out = new ByteArrayOutputStream(); gzip = new GZIPOutputStream(out); byte[] b = new byte[fileIn.available()]; if (-1 == fileIn.read(b)) { return; } IOUtil.close(fileIn); gzip.write(b); IOUtil.close(gzip); byte[] ob = out.toByteArray(); fileOut = new FileOutputStream(destDir + destFileName); fileOut.write(ob); fileOut.flush(); IOUtil.close(fileOut); //deleteFile(srcFileName); } catch (FileNotFoundException e) { } catch (IOException e) { } finally { IOUtil.close(fileIn); IOUtil.close(fileOut); IOUtil.close(gzip); } } /** * * get line number of the file. * @param fileName * @return */ public static int getFileLineNum(String fileName) throws IOException { int result = 0; File file = new File(fileName); //if the source file is empty. if (0 == file.length()) { } String temp = null; BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); while (true) { temp = br.readLine(); if (null != temp) { result++; } else { break; } } } catch (FileNotFoundException e) { } finally { IOUtil.close(br); } return result; } /** * * Add timestamp to file name. * @param srcFileName * @return */ public static String addTime2FileName(String srcFileName) { if (null == srcFileName) { return null; } Date currentDate = new Date(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String currentTimeStr = sdf.format(currentDate); return srcFileName + "." + currentTimeStr; } /** * * Get base directory for download, upload, export. * @return */ public static String getBaseFileDir() { String basePath = System.getProperty("user.dir"); basePath = basePath.substring(0, basePath.length() - 4); return basePath; } /** * * Make directory. * @param destFileDir */ public static void makeDirectory(File destFileDir) { //if target directory dosen't exist, make it. if (!destFileDir.exists()) { if (!destFileDir.mkdirs()) { System.out.println("create dir failed!!!"); } } } /** * * Make directory by name * @param destDirName */ public static void makeDirectory(String destDirName) { File destFileDir = new File(destDirName); makeDirectory(destFileDir); } /** * * Check if file exist in some directory. * @param destDirName * @param destFileName * @return */ public static boolean checkFileExist(String destDirName, String destFileName) { File destDir = new File(destDirName); if (destDir.isDirectory()) { File[] allFiles = destDir.listFiles(); for (int i = 0; i < allFiles.length; i++) { String fileName = allFiles[i].getName(); if (destFileName.equals(fileName) && allFiles[i].isFile()) { return true; } } } return false; } /** * * Get all files from some directory. * @param dirName * @return */ public static List<File> getFileList(String dirName) { File destDir = new File(dirName); List<File> result = new ArrayList<File>(); if (destDir.isDirectory()) { File[] fileArray = destDir.listFiles(); for (File file : fileArray) { if (!file.isDirectory()) { result.add(file); } } } return result; } public static boolean makeFile(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); PrintWriter myFile = new PrintWriter(myFilePath,"UTF-16"); String strContent = fileContent; myFile.println(strContent); myFile.close(); resultFile.close(); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } public static void main(String[] a) { File files = new File("F://报表数据"); if (files.isDirectory()) { File[] file = files.listFiles(); for (File f : file) { FileUtil.renameFile("F://报表数据//" + f.getName(), "F://报表数据//", f.getName() .toUpperCase()); } } } } import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.List; public final class IOUtil { private IOUtil() { } public static void close(InputStream in) { try { if (null != in) { in.close(); } } catch (IOException e) { ; } } public static void close(OutputStream out) { try { if (null != out) { out.close(); } } catch (IOException e) { ; } } public static void close(Reader r) { try { if (null != r) { r.close(); } } catch (IOException e) { ; } } public static void close(Writer w) { try { if (null != w) { w.close(); } } catch (IOException e) { ; } } public static List<String> readFile(String fileName, String charsetName) { BufferedReader br = null; try { if (null == charsetName) { br = new BufferedReader(new FileReader(fileName)); } else { br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charsetName)); } List<String> data = new ArrayList<String>(); String line = null; while (true) { line = br.readLine(); if (null == line) { break; } data.add(line); } return data; } catch (Exception e) { return null; } finally { IOUtil.close(br); } } public static void writeFile(String fileName, List<?> dataList, String charsetName) throws IOException { BufferedWriter bw = null; try { if (null == charsetName) { bw = new BufferedWriter(new FileWriter(fileName)); } else { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), charsetName)); } int size = dataList.size(); for (int i = 0; i < size; i++) { if (i != 0) { bw.newLine(); } bw.write((String)dataList.get(i)); } } catch (Exception e) { } finally { IOUtil.close(bw); } } public static void flush(OutputStream stream) { try { if (null != stream) { stream.flush(); } } catch (IOException e) { ; } } } package swing; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Locale; import java.util.ResourceBundle; public class FileTool { /** * * TODO 添加方法注释 * @param folderPath * @return */ public static boolean newFolder(String folderPath) { try { String filePath = folderPath; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static boolean 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); PrintWriter myFile = new PrintWriter(myFilePath, "GBK"); String strContent = fileContent; myFile.println(strContent); myFile.close(); resultFile.close(); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void makeDir(String path) { try { String tmp[] = path.split("/"); path = tmp[0]; for (int i = 1; i < tmp.length; i++) { path += "/" + tmp[i]; FileTool.newFolder(path); } } catch (Exception ex) { ex.printStackTrace(); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void writeLine(String filePathAndName, String fileContent) { try { String filePath = filePathAndName; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.createNewFile(); } if (myFilePath.canWrite()) { FileWriter resultFile = new FileWriter(myFilePath, true); BufferedWriter myFile = new BufferedWriter(resultFile); String strContent = fileContent; myFile.newLine(); myFile.write(strContent); myFile.newLine(); myFile.write("----------------------------------------"); myFile.close(); resultFile.close(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void writeLineByWriter(String filePathAndName, String fileContent) { try { String filePath = filePathAndName; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.createNewFile(); } if (myFilePath.canWrite()) { FileWriter resultFile = new FileWriter(myFilePath, true); String strContent = fileContent; resultFile.write("/r/n"); resultFile.write(strContent); resultFile.close(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * * 获取指定时间的内容行 * @param folderPath * @return */ public static String getContentWithTime(String filePathAndName,String timeStr) { String filePath = filePathAndName; filePath = filePath.toString(); String tmp = null; File myFilePath = new File(filePath); FileReader fr = null; BufferedReader br = null; try { if (myFilePath.exists()) { fr = new FileReader(myFilePath); br = new BufferedReader(fr); tmp = new String(); while ((tmp = br.readLine()) != null) { if (tmp.indexOf(timeStr) != -1) { return tmp; } } } return null; } catch (Exception ex) { ex.printStackTrace(); return null; } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void writeLineNew(String filePathAndName, String fileContent) { try { String filePath = filePathAndName; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.createNewFile(); } if (myFilePath.canWrite()) { FileWriter resultFile = new FileWriter(myFilePath, true); BufferedWriter myFile = new BufferedWriter(resultFile); String strContent = fileContent; myFile.newLine(); myFile.write(strContent); myFile.newLine(); myFile.write("----------------------------------------"); myFile.close(); resultFile.close(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static boolean delFile(String filePathAndName) { try { String filePath = filePathAndName; filePath = filePath.toString(); java.io.File myDelFile = new java.io.File(filePath); myDelFile.delete(); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * * TODO 添加方法注释 * @param folderPath * @return */ 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 ex) { ex.printStackTrace(); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ 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]);// ��ɾ����ļ��� } } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void copyFile(String oldPath, String newPath) { try { File oldfile = new File(oldPath); if (oldfile.exists()) { // �ļ�����ʱ InputStream inStream = new FileInputStream(oldPath); // ����ԭ�ļ� FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1024]; while (inStream.read(buffer) != -1) { fs.write(buffer); } fs.close(); inStream.close(); } } catch (Exception ex) { // System.out.println("copy error"); ex.printStackTrace(); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static boolean copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); // ����ļ��в����� ��b���ļ��� 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]); } } return true; } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void moveFile(String oldPath, String newPath) { copyFile(oldPath, newPath); delFile(oldPath); } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static void moveFolder(String oldPath, String newPath) { copyFolder(oldPath, newPath); delFolder(oldPath); } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static String readFile(String filePathAndName) { try { String filePath = filePathAndName; filePath = filePath.toString(); String content = null; String tmp = null; File myFilePath = new File(filePath); if (myFilePath.exists()) { FileReader fr = new FileReader(myFilePath); BufferedReader br = new BufferedReader(fr); content = new String(); tmp = new String(); while ((tmp = br.readLine()) != null) { content = content + tmp; } br.close(); fr.close(); } return content; } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * * 判断是否存在指定行的内容 * @param folderPath * @return */ public static boolean existThisTimeLine(String filePathAndName, String timeStr) { String filePath = filePathAndName; filePath = filePath.toString(); String tmp = null; File myFilePath = new File(filePath); FileReader fr = null; BufferedReader br = null; try { if (myFilePath.exists()) { fr = new FileReader(myFilePath); br = new BufferedReader(fr); tmp = new String(); while ((tmp = br.readLine()) != null) { if (tmp.indexOf(timeStr) != -1) { return true; } } } return false; } catch (Exception ex) { ex.printStackTrace(); return false; } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * 查找替换文件内容 * @param folderPath * @return */ public static void readAndReplaceStr(String filePathAndName, String newFileName, String timeStr, String content) { String filePath = filePathAndName; filePath = filePath.toString(); String tmp = null; File myFilePath = new File(filePath); newFile(newFileName, ""); File newFile = new File(newFileName); try { FileWriter fw = new FileWriter(newFile); BufferedWriter bw = new BufferedWriter(fw); if (myFilePath.exists()) { FileReader fr = new FileReader(myFilePath); BufferedReader br = new BufferedReader(fr); tmp = new String(); while ((tmp = br.readLine()) != null) { if (tmp.indexOf(timeStr) != -1) { bw.write(content); } else { bw.write(tmp); } bw.newLine(); } br.close(); fr.close(); bw.close(); fw.close(); } } catch (Exception ex) { ex.printStackTrace(); } finally { delFile(filePathAndName); newFile.renameTo(new File(filePathAndName)); } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static boolean ifFileExists(String filePathAndName) { try { File myFilePath = new File(filePathAndName); if (myFilePath.exists()) { return true; } else { return false; } } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * * TODO 添加方法注释 * @param folderPath * @return */ public static String[] getFileList(String path) { File dir = new File(path); String[] files = dir.list(); return files; } public static String getRandFileName() { return ((Long) System.currentTimeMillis()).toString(); } public static long getFileSize(String pathandname) { File file = new File(pathandname); long temp = file.length(); return temp; } public static ResourceBundle getResourceBundle(String bundleName, String language) { String country; if (language.equals("zh")) country = "CN"; else if (language.equals("en")) country = "US"; else if (language.equals("ja")) country = "JP"; else country = ""; Locale currentLocale; ResourceBundle messages; currentLocale = new Locale(language, country); messages = ResourceBundle.getBundle(bundleName, currentLocale); return messages; } }