📄 fileutil.java
字号:
/*
* $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/wizard/FileUtil.java,v 1.1.1.1 2004/07/01 09:07:54 wang_j Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/07/01 09:07:54 $
*
* ====================================================================
*
* The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
*
* Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
* IT Forest Corporation
* All rights reserved.
*
* This software is the confidential and proprietary information of
* HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
* You shall not disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into with
* HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
*/
package com.webpump.ui.wizard;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Utility class for Files operation
* @author wang_j
* @version 2.0 2004-02-12
*/
public class FileUtil {
/**
* read String from a File object
* @param f File Object
* @return String read File
* @throws IOException
*/
public static String readTextFile(File f) throws IOException {
StringBuffer buf = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String inputLine;
while ((inputLine = in.readLine()) != null) {
buf.append(inputLine);
buf.append('\n');
}
in.close();
return buf.toString();
}
/**
* write String to a File Object
* @param f File Object
* @param content
* @throws IOException
*/
public static void toTextFile(File f, String content) throws IOException {
FileWriter out = new FileWriter(f);
out.write(content);
out.close();
}
/**
* copy file to other file
* @param inputFilename source file
* @param outputFilename target file
* @throws IOException
*/
public static void copy(String inputFilename, String outputFilename) throws IOException {
FileUtil.copy(new File(inputFilename), new File(outputFilename));
}
/**
* copy file(direct) to other file(direct)
* @param input source file(direct)
* @param output target file(object)
* @throws IOException
*/
public static void copy(File input, File output) throws IOException {
if(input.isDirectory() && output.isDirectory()) {
FileUtil.copyDir(input, output);
} else {
FileUtil.copyFile(input, output);
}
}
/**
* copy file to other file
* @param input source file
* @param output target file
* @throws IOException
*/
public static void copyFile(File inputFile, File outputFile) throws IOException{
try
{
BufferedInputStream fr = new BufferedInputStream(new FileInputStream(inputFile));
BufferedOutputStream fw = new BufferedOutputStream(new FileOutputStream(outputFile));
byte[] buf = new byte[8192];
int n;
while((n = fr.read(buf)) >= 0)
fw.write(buf,0,n);
fr.close();
fw.close();
}
catch (Exception e)
{
}
}
/**
* copy direct to other direct
* @param inputDir
* @param outputDir
* @throws IOException
*/
public static void copyDir(File inputDir, File outputDir) throws IOException {
File[] files = inputDir.listFiles();
for(int i=0; i<files.length; i++) {
File destFile = new File(outputDir.getAbsolutePath() + File.separator + files[i].getName());
if(!destFile.exists()) {
if(files[i].isDirectory()) {
destFile.mkdir();
}
}
FileUtil.copy(files[i], destFile);
}
}
/**
* if have a *.extension file at a direct
* @param dir
* @param extension
* @param recursive
* @return
*/
public static boolean dirContainsFiles(File dir, String extension, boolean recursive) {
File[] files = dir.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isFile() && files[i].getName().endsWith(extension))
return true;
if(recursive && files[i].isDirectory())
return FileUtil.dirContainsFiles(files[i], extension, recursive);
}
return false;
}
/**
* read a property's value from an xml file by property
* @param file
* @param property
* @return
* @throws IOException
*/
public static String readPropertyInXMLFile(File file, String property) throws IOException {
String content = FileUtil.readTextFile(file);
int startTagIdx = content.indexOf("<" + property + ">");
int endTagIdx = content.indexOf("</" + property + ">");
if (startTagIdx == -1)
throw new IOException("Property " + property + " not found in file " + file);
return content.substring(startTagIdx + property.length() + 2, endTagIdx);
}
/**
* remove a direct from file system
* @param dir
* @throws IOException
*/
public static void removeDir(File dir) throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory()) {
FileUtil.removeDir(files[i]);
} else {
files[i].delete();
}
}
dir.delete();
}
/**
* remove *.extension file from a direct
* @param dir
* @throws IOException
*/
public static void removeFile(File dir,String extension) throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory()) {
FileUtil.removeFile(files[i],extension);
} else {
if (files[i].getName().endsWith(extension))
files[i].delete();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -