?? fileutil.java
字號:
/*
* $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/FileUtil.java,v 1.14 2004/05/15 19:23:22 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.14 $
* $Date: 2004/05/15 19:23:22 $
*
* ====================================================================
*
* Copyright (C) 2002-2004 by MyVietnam.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* All copyright notices regarding MyVietnam and MyVietnam CoreLib
* MUST remain intact in the scripts and source code.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Correspondence and Marketing Questions can be sent to:
* info@MyVietnam.net
*
* @author: Minh Nguyen minhnn@MyVietnam.net
* @author: Mai Nguyen mai.nh@MyVietnam.net
*/
package net.myvietnam.mvncore.util;
import java.io.*;
import java.net.URL;
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 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 byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] block = new byte[512];
while (true) {
int readLength = inputStream.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 could be used to override the path to WEB-INF/classes
* It can be set when the web app is inited
* @param path String : new path to override the default path
*/
public static void setServletClassesPath(String path) {
log.debug("FileUtil.setServletClassesPath called with path = " + path);
servletClassesPath = path;
if (servletClassesPath.endsWith(File.separator) == false) {
servletClassesPath = servletClassesPath + File.separatorChar;
log.debug("FileUtil.setServletClassesPath change path to value = " + servletClassesPath);
}
}
/**
* This function is used to get the classpath of a reference of one class
* First, this method tries to get the path from system properties
* named "mvncore.context.path" (can be configed in web.xml). If it cannot
* find this parameter, then it will tries to load from the ClassLoader
* @todo FIXME: load from ClassLoader is not correct on Resin/Linux
*/
public static String getServletClassesPath() {
if (servletClassesPath == null) {
String strPath = System.getProperty("mvncore.context.path");
if (strPath != null && (strPath.length() > 0)) {
servletClassesPath = strPath;
} else {
ClassLoader classLoader = instance.getClass().getClassLoader();
URL url = classLoader.getResource("/");
servletClassesPath = url.getPath();
}
log.debug("servletClassesPath = " + servletClassesPath);
if (servletClassesPath.endsWith(File.separator) == false) {
servletClassesPath = servletClassesPath + File.separatorChar;
//log.warn("servletClassesPath does not end with /: " + servletClassesPath);
}
}
return servletClassesPath;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -