星期三, 四月 25, 2007

比较,复制文件,移动文件,重命名文件,文件与String互转,打印文件相关信息

package hartech;

import java.io.*;

/**
*Title: Javas from hartech.cn
*
*Description: JTL's File ToolKit
*Copyright: Copyright (c) 2006 hartech.cn
* Website: www.hartech.cn
*
* @author JTL.zheng@gmail.com
* @version 1.0
*/
public class JFile {

/**
* print the informations of the file input
* @param f File
*/
public static void fileInfo(String name) {
fileInfo(new File(name));
}

public static void fileInfo(File f) {
J.p("--------- Attributes of the files ----------");
if (f == null) {
J.pw("a null reference!!");
}
else if (!f.exists()) {
J.pw(f.toString() + " file Not Found!");
}
else if (f.isFile()) {
StringBuffer length = new StringBuffer(String.valueOf(f.length()));
int i = length.length() - 3;
while (i > 0) {
length.insert(i, ",");
i -= 3;
}
J.p("\"" + f.toString() + "\" is a File.");
J.p("Name: \t\t" + f.getName());
J.p("Readable: \t" + f.canRead());
J.p("Writable: \t" + f.canWrite());
J.p("AbsolutePath:\t" + f.getAbsolutePath());
J.p("Parent:\t\t" + f.getAbsoluteFile().getParent());
J.p("Length:\t\t" + length + " bytes");
}
else {
J.p("\"" + f.toString() + "\" is a Directory");
J.p("Name: \t\t" + f.getName());
J.p("Readable: \t" + f.canRead());
J.p("Writable: \t" + f.canWrite());
J.p("AbsolutePath:\t" + f.getAbsolutePath());
J.p("Parent:\t\t" + f.getAbsoluteFile().getParent());
J.p("Subfiles:\t" + f.list().length);
}
J.p("-------------- fileInfo END ---------------");
}

/**
* compare file byte by byte

* can compare any file(binary file,text file...)
* @param file1 File
* @param file2 File
* @return boolean
*/
public static boolean compareFile(String file1, String file2) {
return compareFile(new File(file1), new File(file2));
}

public static boolean compareFile(File file1, File file2) {
BufferedInputStream in1 = null, in2 = null;
try {
in1 = new BufferedInputStream(new FileInputStream(
file1));
in2 = new BufferedInputStream(new FileInputStream(
file2));
int i;
while ( (i = in1.read()) != -1) {
if (i != in2.read()) {
return false;
}
}
if (in2.read() != -1) {
return false;
}
return true;
}
catch (FileNotFoundException ex) {
J.pw("File not found!");
}
catch (IOException ex) {
J.pw("IOException!");
}
finally {
try {
in1.close();
in2.close();
}
catch (IOException ex1) {
J.pw("IOException when closing!");
}
}
return false;
}

/**
* the java.io.File.renameTo(File,String newname)
* actually move the file to newname's path and rename it which is inconvenient.

* this method just rename the file and keep where it is,ignore the move

* and return the new File's reference


* eg. file = renameFile(file,"NewName.xxx");
* @param file File
* @param name String the newName
* @return File the new File's reference
*/
public static File renameFile(String file, String name) {
return renameFile(new File(file), name);
}

public static File renameFile(File file, String name) {
File newname;
if (file == null !file.exists()) {
J.pw("File not found!");
return null;
}
if (file.getParent() == null) {
newname = new File(name);
file.renameTo(newname);
}
else {
newname = new File(file.getParentFile(), name);
file.renameTo(newname);
}
J.p("Rename is done: " + file + " -> " + newname);
return newname;
}

/**
* use java.io.File.renameTo(File,String newname) to move file


* parameters must be a file and a directory

* return a reference point to the new file
* @param scr String
* @param dir String
* @return File a reference point to the new file
*/
public static File moveFile(String scr, String dir) {
return moveFile(new File(scr), new File(dir));
}

public static File moveFile(File scr, File dir) {
if (scr == null dir == null) {
J.pw("a null reference!");
return null;
}
if (!scr.exists() !dir.exists() scr.isDirectory() dir.isFile()) {
J.pw("not file or directory or not exist!");
return null;
}
File f = new File(dir, scr.getName());
if (f.exists()) {
J.pw("target file has existed!");
}
scr.renameTo(f);
J.p("move file done: " + scr + " -> " + f);
return f;
}

/**
* turn file to String


* maybe you can use it to access file randomly through the string

* but it maybe fault when trun a big file to string
* @param file String
* @return String
*/
public static String fileToString(String file) {
String lineStr = "", string = "";
int i;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));
while ( (i = in.read()) != -1) {
string += (char) i;
}
string = string.trim();
return string;
}
catch (FileNotFoundException ex) {
J.pw("File Not Found!");
}
catch (IOException ex) {
J.pw("IO exception!");
}
finally {
try {
in.close();
}
catch (IOException ex1) {
J.pw("IOException when closing!");
}
}
return null;
}

/**
* write the string to file

* if fail return false else return true
* @param src String
* @param file String
* @return boolean
*/
public static boolean stringToFile(String src, String file) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(file));
out.write(src);
return true;
}
catch (Exception ex) {
J.pw("IO exception!");
}
finally {
try {
out.close();
}
catch (IOException ex) {
J.pw("IOException when closing!");
}
}
return false;
}

/**
* only used to copy character files

* local char -> int -> unicode -> int -> local char
* @param src String
* @param dest String
*/
static public void copyFileByChar(String src, String dest) {
String lineStr;
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(src));
out = new BufferedWriter(new FileWriter(dest));
while ( (lineStr = in.
readLine()) != null) {
out.write(lineStr);
out.newLine();
}
J.p("copy is done !");
}
catch (FileNotFoundException ex) {
J.pw("File Not Found!");
}
catch (IOException ex) {
J.pw("IO exception!");
}
finally {
try {
in.close();
out.close();
}
catch (IOException ex1) {
J.pw("IOException when closing!");
}
}

}

/**
* copy file by byte

* can copy any file,because any file is made of bytes

* bytes -> int -> bytes
* @param src String
* @param dest String
*/
public static void copyFile(String src, String dest) {
copyFile(new File(src), new File(dest));
}

public static void copyFile(File src, File dest) {
int b;
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
out = new BufferedOutputStream(new FileOutputStream(
dest));
while ( (b = in.read()) != -1) {
out.write(b);
}
J.p("CopyFile is done: " + src + " -> " + dest);
}
catch (FileNotFoundException ex) {
J.pw("File Not Found!");
}
catch (IOException ex) {
J.pw("IO exception!");
}
finally {
try {
in.close();
out.close();
}
catch (IOException ex1) {
J.pw("IOException when closing!");
}
}
}

static public void transformFile(String src, String dest) {
String lineStr;
int i = 0;
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(src));
out = new BufferedWriter(new FileWriter(dest));
while ( (lineStr = in.
readLine()) != null) {
// add transform codes here
// line by line
// eg. String lineStr=function(lineStr)
i = 0;
if (!lineStr.equals("")) {
while (lineStr.charAt(i) == J.SPACEC) {
lineStr = lineStr.replaceFirst(J.SPACES, J.TABS);
i++;
}
}
// code end
out.write(lineStr);
out.newLine();
}
J.p("Transform File is done: " + src + " -> " + dest);
}
catch (FileNotFoundException ex) {
J.pw("File Not Found!");
}
catch (IOException ex) {
J.pw("IO exception!");
}
finally {
try {
in.close();
out.close();
}
catch (IOException ex) {
J.pw("IOException when closing!");
}
}
}

public static String getIO() {
java.io.BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
try {
return br.readLine();
}
catch (IOException ex) {
return null;
}
}

public static void main(String[] arg) {
transformFile("scr/hartech/J.java", "dest.txt");
}
}

没有评论: