/**
* Script Name : <b>FileRobot</b> <br>
* Description : The operation on file <br>
* Type : Java <br>
* Generated : <b>2006-5-26 13:49:00</b> <br>
* Original Host : WinNT Version 5.1 Build 2600 (S)
*
* @since 2006/05/26
* @author Jaytse
*/
public class FileRobot {
/**
* Write some info to a file
*
* @param filePath
* the filepath of the file
* @param fileName
* the name of the file
* @param contents
* the information wanted to writed in
* @throws IOException
*/
public static void writeFile(String filePath, String fileName,
String[] contents) throws IOException {
FileWriter fw = new FileWriter(filePath + fileName);
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i < contents.length; i++) {
out.write(contents[i]);
out.println();
out.flush();
}
fw.close();
out.close();
}
/**
* Write some info to a file
*
* @param filePath
* the filepath of the file
* @param fileName
* the name of the file
* @param content
* the information wanted to writed in
* @throws IOException
*/
public static void writeFile(String filePath, String fileName,
String content) throws IOException {
String[] contents = new String[1];
contents[0] = content;
writeFile(filePath, fileName, contents);
}
/**
* Create and Delete a file
*
* @param filePath
* the path of file
* @param fileName
* the name of file
* @return if operation success, return true
* @throws IOException
*/
public static boolean createAndDeleteFile(String filePath, String fileName)
throws IOException {
boolean result = false;
File file = new File(filePath, fileName);
if (file.exists()) {
file.delete();
result = true;
System.out.println("File has been Delete!");
} else {
file.createNewFile();
result = true;
System.out.println("File has been Create!");
}
return result;
}
/**
* Create and Delete a folder
*
* @param folderName
* the folder name
* @param filePath
* the fold path
* @return if operation success, return true
*/
public static boolean createAndDeleteFolder(String folderName,
String filePath) {
boolean result = false;
try {
File file = new File(filePath + folderName);
if (file.exists()) {
file.delete();
System.out.println("Folder exists and it has been delete!");
result = true;
} else {
file.mkdir();
System.out
.println("Folder does not exist and it has been created!");
result = true;
}
} catch (Exception ex) {
result = false;
System.out.println("CreateAndDeleteFolder is error:" + ex);
}
return result;
}
/**
* Create a file with its name
* @param fileName the file name
*/
public static void makeFile(String fileName) {
String dir = fileName.substring(0, fileName.lastIndexOf("//"));
String name = fileName.substring(fileName.lastIndexOf("//") + 1,
fileName.length());
makeFile(dir, name);
}
/**
* Create a file with its name, and its directory name
* @param dirName the directory name
* @param fileName the file name
*/
public static void makeFile(String dirName, String fileName) {
try {
Stack stack = new Stack();
String old = dirName;
String hhh = old;
while (hhh.indexOf("//") != -1) {
if (!hhh.equals(old)) {
stack.push(hhh);
}
stack.push(hhh.substring(hhh.lastIndexOf("//") + 1));
hhh = hhh.substring(0, hhh.lastIndexOf("//"));
}
stack.push(hhh);
int size = stack.size() / 2;
for (int i = 0; i < size; i++) {
File file = new File((String) stack.pop() + "//"
+ (String) stack.pop());
file.mkdir();
}
File file = new File(dirName + "//" + fileName);
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Print folder and file list at the filepath
*
* @param filePath
* the destinated filepath
*/
public static void readFolderByFile(String filePath) {
File file = new File(filePath);
File[] tempFile = file.listFiles();
for (int i = 0; i < tempFile.length; i++) {
if (tempFile[i].isFile()) {
System.out.println("File : " + tempFile[i].getName());
}
if (tempFile[i].isDirectory()) {
System.out.println("Directory : " + tempFile[i].getName());
}
}
}
/**
* chech whether the file content is empty
*
* @param filePath
* the path of the file
* @param fileName
* the name of the file
* @return when the content is empty, return true
* @throws IOException
*/
public static boolean fileIsNull(String filePath, String fileName)
throws IOException {
boolean result = false;
FileReader fr = new FileReader(filePath + fileName);
if (fr.read() == -1) {
result = true;
System.out.println(fileName + " has no data!");
} else {
System.out.println(fileName + " has some data!");
}
fr.close();
return result;
}
/**
* Get the content of the file
*
* @param filePath
* the path of the file
* @param fileName
* the name of the file
* @return the content of the file
* @throws IOException
*/
public static String readAllFile(String filePath, String fileName)
throws IOException {
StringBuffer buffer = new StringBuffer();
FileReader fr = new FileReader(filePath + fileName);
int count = fr.read();
while (count != -1) {
buffer.append((char) count);
count = fr.read();
if (count == 13) {
fr.skip(1);
}
}
fr.close();
return buffer.toString();
}
/**
* Get the content of the file
*
* @param filePath
* the path of the file
* @param fileName
* the name of the file
* @return the file content
* @throws IOException
*/
public static String readLineFile(String filePath, String fileName)
throws IOException {
return readLineFile(filePath + fileName);
}
/**
* Get the content of the file
* @param fileFullName the file name
* @return the file content
* @throws IOException
*/
public static String readLineFile(String fileFullName) throws IOException {
StringBuffer buffer = new StringBuffer();
FileReader fr = new FileReader(fileFullName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
buffer.append(line);
line = br.readLine();
}
br.close();
fr.close();
return buffer.toString();
}
/**
* Get files for process
* @param file the directory name
* @param type the file type
* @return the sub-directory and the files
*/
public static File[] getFilesWithFilter(File file, String type){
File[] list = file.listFiles();
Vector v = new Vector();
for (int i = 0; i < list.length; i++) {
if(list[i].isDirectory()){
if(checkDirFile(list[i], type)){
v.addElement(list[i]);
}
}else{
if(list[i].getName().endsWith("." + type)){
v.addElement(list[i]);
}
}
}
File[] Result = new File[v.size()];
for (int i = 0; i < Result.length; i++) {
Result[i] = (File) v.elementAt(i);
}
return Result;
}
/**
* Check whether a dir contains a file
* @param file directory name
* @param type file name
* @return true if yes
*/
public static boolean checkDirFile(File file, String type){
boolean bResult = false;
if(file.isDirectory()){
File[] list = file.listFiles();
for(int i = 0 ; i<list.length; i++){
if(list[i].isDirectory()){
bResult |= checkDirFile(list[i], type);
}else{
bResult |= list[i].getName().endsWith("." + type);
}
}
}
return bResult;
}
}