📄 fileutil.java
字号:
/**
* 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[4096];
try {
input = new BufferedInputStream(new FileInputStream(srcFile), 4096);
while (true) {
int length = input.read(block);
if (length == -1) break;// end of file
output.write(block, 0, length);
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ex) {
// just ignore
}
}
}
}
/**
* 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 == null) {
// From mvnForum.com thread 2243:
// I am deploying the MVNForum as an ear in Linux box so context real path turns out to be null.
return;
}
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("/");
if (url == null) {
// not run on the Servlet environment
servletClassesPath = ".";
} else {
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;
}
/**
* This method create a file text/css
* NOTE: This method closes the inputStream after it have done its work.
*
* @param inputStream the stream of a text/css file
* @param cssFile the output file, have the ".css" extension or orther extension
* @throws IOException
* @throws BadInputException
* @throws AssertionException
*/
public static void createTextFile(InputStream inputStream, String textFile)
throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("Does not accept null input");
}
OutputStream outputStream = null;
try {
byte[] srcByte = FileUtil.getBytes(inputStream);
outputStream = new FileOutputStream(textFile);
outputStream.write(srcByte);
return;
} catch (IOException e) {
log.error("Error", e);
throw e;
} finally { // this finally is very important
inputStream.close();
if (outputStream != null) outputStream.close();
}
}
/**
* Write content to a fileName with the destEncoding
*
* @param content String
* @param fileName String
* @param destEncoding String
* @throws FileNotFoundException
* @throws IOException
*/
public static void writeFile(String content, String fileName, String destEncoding)
throws FileNotFoundException, IOException {
File file = null;
try {
file = new File(fileName);
if (file.isFile() == false) {
throw new IOException("'" + fileName + "' is not a file.");
}
if (file.canWrite() == false) {
throw new IOException("'" + fileName + "' is a read-only file.");
}
} finally {
// we dont have to close File here
}
BufferedWriter out = null;
try {
FileOutputStream fos = new FileOutputStream(fileName);
out = new BufferedWriter(new OutputStreamWriter(fos, destEncoding));
out.write(content);
out.flush();
} catch (FileNotFoundException fe) {
log.error("Error", fe);
throw fe;
} catch (IOException e) {
log.error("Error", e);
throw e;
} finally {
try {
if (out != null) out.close();
} catch (IOException ex) {}
}
}
public static String readFile(String fileName, String srcEncoding)
throws FileNotFoundException, IOException {
File file = null;
try {
file = new File(fileName);
if (file.isFile() == false) {
throw new IOException("'" + fileName + "' is not a file.");
}
} finally {
// we dont have to close File here
}
BufferedReader reader = null;
try {
StringBuffer result = new StringBuffer(1024);
FileInputStream fis = new FileInputStream(fileName);
reader = new BufferedReader(new InputStreamReader(fis, srcEncoding));
char[] block = new char[512];
while (true) {
int readLength = reader.read(block);
if (readLength == -1) break;// end of file
result.append(block, 0, readLength);
}
return result.toString();
} catch (FileNotFoundException fe) {
log.error("Error", fe);
throw fe;
} catch (IOException e) {
log.error("Error", e);
throw e;
} finally {
try {
if (reader != null) reader.close();
} catch (IOException ex) {}
}
}
/*
* 1 ABC
* 2 abC Gia su doc tu dong 1 lay ca thay 5 dong => 1 --> 5
* 3 ABC
*/
public static String[] getLastLines(File file, int linesToReturn)
throws IOException, FileNotFoundException {
final int AVERAGE_CHARS_PER_LINE = 250;
final int BYTES_PER_CHAR = 2;
RandomAccessFile randomAccessFile = null;
StringBuffer buffer = new StringBuffer(linesToReturn * AVERAGE_CHARS_PER_LINE);
int lineTotal = 0;
try {
randomAccessFile = new RandomAccessFile(file, "r");
long byteTotal = randomAccessFile.length();
long byteEstimateToRead = linesToReturn * AVERAGE_CHARS_PER_LINE * BYTES_PER_CHAR;
long offset = byteTotal - byteEstimateToRead;
if (offset < 0) {
offset = 0;
}
randomAccessFile.seek(offset);
//log.debug("SKIP IS ::" + offset);
String line = null;
String lineUTF8 = null;
while ((line = randomAccessFile.readLine()) != null) {
lineUTF8 = new String(line.getBytes("ISO8859_1"), "UTF-8");
lineTotal++;
buffer.append(lineUTF8).append("\n");
}
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException ex) {
}
}
}
String[] resultLines = new String[linesToReturn];
BufferedReader in = null;
try {
in = new BufferedReader(new StringReader(buffer.toString()));
int start = lineTotal /* + 2 */ - linesToReturn; // Ex : 55 - 10 = 45 ~ offset
if (start < 0) start = 0; // not start line
for (int i = 0; i < start; i++) {
in.readLine(); // loop until the offset. Ex: loop 0, 1 ~~ 2 lines
}
int i = 0;
String line = null;
while ((line = in.readLine()) != null) {
resultLines[i] = line;
i++;
}
} catch (IOException ie) {
log.error("Error" + ie);
throw ie;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
}
return resultLines;
}
public static String getHumanSize(long size) {
int sizeToStringLength = String.valueOf(size).length();
String humanSize = "";
DecimalFormat formatter = new DecimalFormat("##0.##");
if (sizeToStringLength > 9) {
humanSize += formatter.format((double) size / (1024 * 1024 * 1024)) + " GB";
} else if (sizeToStringLength > 6) {
humanSize += formatter.format((double) size / (1024 * 1024)) + " MB";
} else if (sizeToStringLength > 3) {
humanSize += formatter.format((double) size / 1024) + " KB";
} else {
humanSize += String.valueOf(size) + " Bytes";
}
return humanSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -