📄 files.java
字号:
try { fileOut = new FileOutputStream(file); bufOut = new BufferedOutputStream(fileOut); objOut = new ObjectOutputStream(bufOut); objOut.writeObject(object); } finally { Streams.closeOutputStream(objOut); Streams.closeOutputStream(bufOut); Streams.closeOutputStream(fileOut); } } /** * Returns the name of a file before the final period separator. * * @param name Name of file. * @return File name's prefix. */ public static String prefix(String name) { int lastDotIndex = name.lastIndexOf('.'); if (lastDotIndex < 0) return name; return name.substring(0,lastDotIndex); } /** * Returns the name of a file after the final period separator. * * @param name Name of file. * @return File name's suffix. */ public static String suffix(String name) { int lastDotIndex = name.lastIndexOf('.'); if (lastDotIndex < 0) return ""; if (lastDotIndex >= name.length()) return ""; return name.substring(lastDotIndex+1); } /** * Returns prefix of the file's name. * * @param file File whose name's prefix is returned. * @return Prefix of file. */ public static String baseName(File file) { return prefix(file.getName()); } /** * Removes the specified file and if it is a directory, all * contained files. Returns number of files removed, including * specified one. * * @param file File or directory to remove. * @return Number of files and directories removed. */ public static int removeRecursive(File file) { if (file == null) return 0; // nothing to remove int descCount = removeDescendants(file); file.delete(); return descCount + 1; } /** * Remove the descendants of the specified directory, but not the * directory itself. Returns number of files removed. * * @param file File whose descendants are removed. * @return Number of files or directories removed. */ public static int removeDescendants(File file) { if (!file.isDirectory()) return 0; int count = 0; File[] files = file.listFiles(); for (int i = 0; i < files.length; ++i) count += removeRecursive(files[i]); return count; } /** * Creates a clean directory with the specified name * as a subdirectory of the specified directory. * * @param directory Parent directory of directory to create. * @param name Name of directory to create. * @return Directory with specified name that is daughter of * specified parent directory. */ public static File makeCleanDir(File directory, String name) { return makeCleanDir(new File(directory,name)); } /** * Creates a directory in the specified file that has no * subdirectories. If the specified file exists as a file, it * is removed first. If the specified file is a directory with * files or subdireictories, they are removed. * * @param file File to create as a clean directory. * @return Directory with specified name that is daughter of * specified parent directory. */ public static File makeCleanDir(File file) { if (file.exists() & !file.isDirectory()) file.delete(); if (!file.isDirectory()) file.mkdir(); removeDescendants(file); return file; } /** * Returns a new file with the specified name that is a daughter * of the temporary directory {@link #TEMP_DIRECTORY}. The file * will be deleted automicatically when the virtual machine exits. * * @param fileName Name of file to create. * @return Temporary file with specified name. */ public static File createTempFile(String fileName) { File tempFile = new File(TEMP_DIRECTORY,fileName); tempFile.deleteOnExit(); return tempFile; } /** * Converts a file to the string representation of its URL, * consisting of the URL prefix and a canonical path name. * * @param file File to convert to string URL. * @return The string URL corresponding to the specified file. * @throws IOException If there is an exception retrieving the * file's canonical path name. */ public static String fileToURLName(File file) throws IOException { return FILE_URL_PREFIX + file.getCanonicalPath(); } /** * Returns the lines of a file as an array of strings. The * newline characters are removed. * * @param file Name of file from which to read lines. * @param charset Character set to use to read lines. * @return Array of lines read from file. * @throws IOException If there is an I/O exception opening, * closing, or reading from the file. */ public static String[] readLinesFromFile(File file, String charset) throws IOException { FileInputStream in = null; InputStreamReader streamReader = null; BufferedReader reader = null; try { in = new FileInputStream(file); streamReader = new InputStreamReader(in,charset); reader = new BufferedReader(streamReader); ArrayList list = new ArrayList(); String line; while ((line= reader.readLine()) != null) { list.add(line); } return Collections.toStringArray(list); } finally { Streams.closeInputStream(in); Streams.closeReader(streamReader); Streams.closeReader(reader); } } /** * Serializes the specifies object and returns its deserialized * version. This operates in memory by writing to a byte-array * backed object output stream and reading from a byte-array * backed object input stream. * * @param in Object to serialize and then deserialize. * @return Deserialized version of serialized version of specified * object. * @throws IOException If there is an I/O error while reading or * writing. * @throws ClassNotFoundException If the class for the object * being restored cannot be found. * */ public static Object serializeDeserialize(Serializable in) throws ClassNotFoundException, IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); ObjectOutputStream objOut = new ObjectOutputStream(bytesOut); objOut.writeObject(in); byte[] bytes = bytesOut.toByteArray(); ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes); ObjectInputStream objIn = new ObjectInputStream(bytesIn); return objIn.readObject(); } /** * Copies the contents of one file into another. Even if there * is an exception, both file handles will be closed on exit. * * @param from File from which to copy. * @param to File to which to copy. * @throws IOException If there is a read or write error. */ public static void copyFile(File from, File to) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(from); out = new FileOutputStream(to); byte[] bytes = new byte[1024*4]; int len = 0; while ((len = in.read(bytes)) >= 0) out.write(bytes,0,len); } finally { Streams.closeInputStream(in); Streams.closeOutputStream(out); } } /** * Prefix for file names to convert them into URLs. */ private static String FILE_URL_PREFIX = "file:///"; /** * Name of the property for the system temporary directory. */ private static final String TEMP_DIRECTORY_SYS_PROPERTY = "java.io.tmpdir"; /** * The temporary directory. */ public static final File TEMP_DIRECTORY = new File(System.getProperty(TEMP_DIRECTORY_SYS_PROPERTY)); /** * A file filter that accepts files that are directories * that are not named "CVS", ignoring case. */ public static final FileFilter NON_CVS_DIRECTORY_FILE_FILTER = new FileFilter() { public boolean accept(File file) { return file.isDirectory() && !file.getName().equalsIgnoreCase("CVS"); } }; /** * A file filter that accepts all normal files, as * specified by {@link File#isFile()}. */ public static final FileFilter FILES_ONLY_FILE_FILTER = new FileFilter() { public boolean accept(File file) { return file.isFile(); } };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -