?? fileutil.java
字號:
/*
* $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/FileUtil.java,v 1.39 2006/04/15 02:59:20 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.39 $
* $Date: 2006/04/15 02:59:20 $
*
* ====================================================================
*
* Copyright (C) 2002-2006 by MyVietnam.net
*
* All copyright notices regarding MyVietnam and MyVietnam CoreLib
* MUST remain intact in the scripts and source code.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Correspondence and Marketing Questions can be sent to:
* info at MyVietnam net
*
* @author: Minh Nguyen
* @author: Mai Nguyen
*/
package net.myvietnam.mvncore.util;
import java.io.*;
import java.net.URL;
import java.text.DecimalFormat;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.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[4096];
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;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -