package application.util; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; public class FileUtils { private static File justVisitedDir; public static void toClipboard(String message) { Clipboard clipboard = Clipboard.getSystemClipboard(); ClipboardContent clipboardContent = new ClipboardContent(); clipboardContent.putString(message); clipboard.setContent(clipboardContent); } public static boolean isImage(File file){ Pattern pattern = Pattern.compile(".*(.jpg|.JPG|.jpeg|.JPEG|.png|.PNG|.gif|.GIF|.bmp|.BMP)$"); Matcher matcher = pattern.matcher(file.getName()); return matcher.matches(); } public static String read(String path){ try { FileInputStream fileInputStream = new FileInputStream(new File(path)); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "utf-8")); String line = null; StringBuffer buffer = new StringBuffer(); while((line = bufferedReader.readLine()) != null){ buffer.append(line + "\r\n"); } bufferedReader.close(); fileInputStream.close(); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } public static boolean writeFile(String filePath,byte[] buffer) { boolean flag = false; File file = new File(filePath); FileOutputStream out = null; try { out = new FileOutputStream(file); out.write(buffer); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return flag; } public static File chooseFile(){ FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("��ѡ���ļ�"); if(justVisitedDir == null) { // ���ó�ʼĿ¼ fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); } else { fileChooser.setInitialDirectory(justVisitedDir); } fileChooser.getExtensionFilters().addAll(new ExtensionFilter[]{new ExtensionFilter("Image Files", new String[]{"*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp"}), new ExtensionFilter("JPG", new String[]{"*.jpg"}), new ExtensionFilter("JPEG", new String[]{"*.jpeg"}), new ExtensionFilter("BMP", new String[]{"*.bmp"}), new ExtensionFilter("PNG", new String[]{"*.png"}), new ExtensionFilter("GIF", new String[]{"*.gif"})}); File file = fileChooser.showOpenDialog(null); if(file != null){ justVisitedDir = file.getParentFile(); } return file; } public static void openFile(File file) { if(file != null && file.exists()) { try { Runtime.getRuntime().exec("cmd.exe /c start \" \" \"" + file.getAbsolutePath() + "\""); } catch (IOException e) { e.printStackTrace(); } } } public static File createFile(String folderPath, String fileName) { File destDir = new File(folderPath); if (!destDir.exists()) { destDir.mkdirs(); } return new File(folderPath, fileName + fileName); } public static String getFileName(String filePath) { return filePath.substring(filePath.lastIndexOf(File.separator) + 1); } public static String getFileNameNoSuffix(String filePath) { int point = filePath.lastIndexOf('.'); return filePath.substring(filePath.lastIndexOf(File.separator) + 1,point); } public static String getFileSuffix(String fileName) { int point = fileName.lastIndexOf('.'); return fileName.substring(point + 1); } public static long getFileSize(String filePath) { long size = 0; File file = new File(filePath); if (file != null && file.exists()) { size = file.length(); } return size; } public static String getFileSize(long size) { if (size <= 0) return "0"; java.text.DecimalFormat df = new java.text.DecimalFormat("##.##"); float temp = (float) size / 1024; if (temp >= 1024) { return df.format(temp / 1024) + "M"; } else { return df.format(temp) + "K"; } } public static String formatFileSize(long fileSize) { java.text.DecimalFormat df = new java.text.DecimalFormat("#.00"); String fileSizeString = ""; if (fileSize < 1024) { fileSizeString = df.format((double) fileSize) + "B"; } else if (fileSize < 1048576) { fileSizeString = df.format((double) fileSize / 1024) + "KB"; } else if (fileSize < 1073741824) { fileSizeString = df.format((double) fileSize / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileSize / 1073741824) + "G"; } return fileSizeString; } public static long getDirSize(File dir) { if (dir == null) { return 0; } if (!dir.isDirectory()) { return 0; } long dirSize = 0; File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { dirSize += file.length(); } else if (file.isDirectory()) { dirSize += file.length(); dirSize += getDirSize(file); // �ݹ��ü��ͳ�� } } return dirSize; } public long getFileList(File dir) { long count = 0; File[] files = dir.listFiles(); count = files.length; for (File file : files) { if (file.isDirectory()) { count = count + getFileList(file);// �ݹ� count--; } } return count; } public static byte[] toBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) { out.write(ch); } byte buffer[] = out.toByteArray(); out.close(); return buffer; } public static boolean checkFileExists(String path,String name) { boolean status; if (!name.equals("")) { File newPath = new File(path.toString() + name); status = newPath.exists(); } else { status = false; } return status; } public static boolean checkFilePathExists(String path) { return new File(path).exists(); } public static boolean createDirectory(String path,String directoryName) { boolean status; if (!directoryName.equals("")) { File newPath = new File(path.toString() + directoryName); status = newPath.mkdir(); status = true; } else status = false; return status; } public static boolean deleteDirectory(String path,String fileName) { boolean status; SecurityManager checker = new SecurityManager(); if (!fileName.equals("")) { File newPath = new File(path.toString() + fileName); checker.checkDelete(newPath.toString()); if (newPath.isDirectory()) { String[] listfile = newPath.list(); try { for (int i = 0; i < listfile.length; i++) { File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString()); deletedFile.delete(); } newPath.delete(); System.out.println("DirectoryManager deleteDirectory"+ fileName); status = true; } catch (Exception e) { e.printStackTrace(); status = false; } } else status = false; } else status = false; return status; } public static int deleteBlankPath(String path) { File f = new File(path); if (!f.canWrite()) { return 1; } if (f.list() != null && f.list().length > 0) { return 2; } if (f.delete()) { return 0; } return 3; } public static boolean reNamePath(String oldName, String newName) { File f = new File(oldName); return f.renameTo(new File(newName)); } public static boolean deleteFileWithPath(String filePath) { SecurityManager checker = new SecurityManager(); File f = new File(filePath); checker.checkDelete(filePath); if (f.isFile()) { System.out.println("DirectoryManager deleteFile"+filePath); f.delete(); return true; } return false; } public static void clearFileWithPath(String filePath) { List<File> files = FileUtils.listPathFiles(filePath); if (files.isEmpty()) { return; } for (File f : files) { if (f.isDirectory()) { clearFileWithPath(f.getAbsolutePath()); } else { f.delete(); } } } public static List<String> listPath(String root) { List<String> allDir = new ArrayList<String>(); SecurityManager checker = new SecurityManager(); File path = new File(root); checker.checkRead(root); // ���˵���.��ʼ���ļ��� if (path.isDirectory()) { for (File f : path.listFiles()) { if (f.isDirectory() && !f.getName().startsWith(".")) { allDir.add(f.getAbsolutePath()); } } } return allDir; } public static List<File> listPathFiles(String root) { List<File> allDir = new ArrayList<File>(); SecurityManager checker = new SecurityManager(); File path = new File(root); checker.checkRead(root); File[] files = path.listFiles(); for (File f : files) { if (f.isFile()) allDir.add(f); else listPath(f.getAbsolutePath()); } return allDir; } public enum PathStatus { SUCCESS, EXITS, ERROR } public static PathStatus createPath(String newPath) { File path = new File(newPath); if (path.exists()) { return PathStatus.EXITS; } if (path.mkdir()) { return PathStatus.SUCCESS; } else { return PathStatus.ERROR; } } public static String getPathName(String absolutePath) { int start = absolutePath.lastIndexOf(File.separator) + 1; int end = absolutePath.length(); return absolutePath.substring(start, end); } }