📄 fileutil.java
字号:
/* * FileUtil.java * * Created on 2006年7月25日, 上午11:26 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package tot.util;import java.io.*;import java.net.URL;import java.text.DecimalFormat;import tot.exception.BadInputException;import tot.filter.DisableHtmlTagFilter;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public final class FileUtil { private static Log log = LogFactory.getLog(FileUtil.class); private static FileUtil instance = new FileUtil(); private static String servletClassesPath = null; private FileUtil() { // prevent instantiation } public static void checkGoodFilePath(String str) throws BadInputException { byte[] s = str.getBytes(); int length = s.length; byte b = 0; for (int i = 0; i < length; i++) { b = s[i]; if ((b == '*') || (b == '?') || (b == '<') || (b == '>') || (b == '"') || (b == '|') || (b == '\0')) {//null char : is it correct ???? // not good char, throw an BadInputException //@todo : localize me throw new BadInputException("The string '" + DisableHtmlTagFilter.filter(str) + "' is not a good file path. Reason: character '" + (char)(b) + "' is not allowed."); } }// for } public static void checkGoodFileName(String str) throws BadInputException { // must be a good file path first checkGoodFilePath(str); byte[] s = str.getBytes(); int length = s.length; byte b = 0; for (int i = 0; i < length; i++) { b = s[i]; if ((b == '/') || (b == '\\') || (b == ':')) { // not good char, throw an BadInputException //@todo : localize me throw new BadInputException("The string '" + DisableHtmlTagFilter.filter(str) + "' is not a good file name. Reason: character '" + (char)(b) + "' is not allowed."); } }// for } public static void createDir(String dir, boolean ignoreIfExitst) throws IOException { File file = new File(dir); if (ignoreIfExitst && file.exists()) { return; } if ( file.mkdir() == false) { throw new IOException("Cannot create the directory = " + dir); } } public static void createDirs(String dir, boolean ignoreIfExitst) throws IOException { File file = new File(dir); if (ignoreIfExitst && file.exists()) { return; } if ( file.mkdirs() == false) { throw new IOException("Cannot create directories = " + dir); } } public static void deleteFile(String filename) throws IOException { File file = new File(filename); log.trace("Delete file = " + filename); if (file.isDirectory()) { throw new IOException("IOException -> BadInputException: not a file."); } if (file.exists() == false) { throw new IOException("IOException -> BadInputException: file is not exist."); } if (file.delete() == false) { throw new IOException("Cannot delete file. filename = " + filename); } } public static void deleteDir(File dir) throws IOException { if (dir.isFile()) throw new IOException("IOException -> BadInputException: not a directory."); File[] files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isFile()) { file.delete(); } else { deleteDir(file); } } }//if dir.delete(); } public static long getDirLength(File dir) throws IOException { if (dir.isFile()) throw new IOException("BadInputException: not a directory."); long size = 0; File[] files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; long length = 0; if (file.isFile()) { length = file.length(); } else { length = getDirLength(file); } size += length; }//for }//if return size; } public static long getDirLength_onDisk(File dir) throws IOException { if (dir.isFile()) throw new IOException("BadInputException: not a directory."); long size = 0; File[] files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; long length = 0; if (file.isFile()) { length = file.length(); } else { length = getDirLength_onDisk(file); } double mod = Math.ceil(((double)length)/512); if (mod == 0) mod = 1; length = ((long)mod) * 512; size += length; } }//if return size; } public static void emptyFile(String srcFilename) throws IOException { File srcFile = new File(srcFilename); if (!srcFile.exists()) { throw new FileNotFoundException("Cannot find the file: " + srcFile.getAbsolutePath()); } if (!srcFile.canWrite()) { throw new IOException("Cannot write the file: " + srcFile.getAbsolutePath()); } FileOutputStream outputStream = new FileOutputStream(srcFilename); outputStream.close(); } public static void copyFile(String srcFilename, String destFilename, boolean overwrite) throws IOException { File srcFile = new File(srcFilename); if (!srcFile.exists()) { throw new FileNotFoundException("Cannot find the source file: " + srcFile.getAbsolutePath()); } if (!srcFile.canRead()) { throw new IOException("Cannot read the source file: " + srcFile.getAbsolutePath()); } File destFile = new File(destFilename); if (overwrite == false) { if (destFile.exists()) return; } else { if (destFile.exists()) { if (!destFile.canWrite()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } else { if (!destFile.createNewFile()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } } BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; byte[] block = new byte[1024]; try { inputStream = new BufferedInputStream(new FileInputStream(srcFile)); outputStream = new BufferedOutputStream(new FileOutputStream(destFile)); while (true) { int readLength = inputStream.read(block); if (readLength == -1) break;// end of file outputStream.write(block, 0, readLength); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { // just ignore } } if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { // just ignore } } } } //@todo: why this method does not close the inputStream ??? public static byte[] getBytes(InputStream inputStream) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); byte[] block = new byte[512]; while (true) { int readLength = bufferedInputStream.read(block); if (readLength == -1) break;// end of file byteArrayOutputStream.write(block, 0, readLength); } byte[] retValue = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); return retValue; } public static String getFileName(String fullFilePath) { if (fullFilePath == null) { return ""; } int index1 = fullFilePath.lastIndexOf('/'); int index2 = fullFilePath.lastIndexOf('\\'); //index is the maximum value of index1 and index2 int index = (index1 > index2) ? index1 : index2; if (index == -1) { // not found the path separator return fullFilePath; } String fileName = fullFilePath.substring(index + 1); return fileName; } /** * This method write srcFile to the output, and does not close the output * @param srcFile File the source (input) file * @param output OutputStream the stream to write to, this method will not buffered the output * @throws IOException */ public static void popFile(File srcFile, OutputStream output) throws IOException { BufferedInputStream input = null; byte[] block = new byte[1024]; try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -