锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
]]>
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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* 鏂囦歡鎿嶄綔綾?涓昏瀵規(guī)湇鍔$鐨勭増鏈▼搴忚繘琛屾洿鏂?br> * @author frienfship
*
*/
public class FileOperate {
private String message;
public FileOperate() {
}
/**
* 鍙栧緱鐩綍鐨勭粷瀵硅礬寰?br> * @return 榪斿洖緇濆璺緞
*/
public static String getAbsolutePath(){
File file = new File("ecq");
file.mkdir();
String path=file.getAbsolutePath();
return path;
}
/**
* 閲嶅懡鍚嶆枃浠跺す
* @param path 鍘熸枃浠跺す璺緞
* @param newname 鏂板悕縐?br> * @return
*/
public static void renamePath(String path, String newname){
File file = new File(path);
String oldpath = path;
int point = oldpath.lastIndexOf('/');
String beyondpath = oldpath.substring(0, point);
String newpath = beyondpath + newname;
File newfile = new File(newpath);
file.renameTo(newfile);
// file.mkdirs();
}
/**
* 璇誨彇鏂囨湰鏂囦歡鍐呭
* @param filePathAndName 甯︽湁瀹屾暣緇濆璺緞鐨勬枃浠跺悕
* @param encoding 鏂囨湰鏂囦歡鎵撳紑鐨勭紪鐮佹柟寮?br> * @return 榪斿洖鏂囨湰鏂囦歡鐨勫唴瀹?br> */
public String readTxt(String filePathAndName, String encoding)
throws IOException {
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
String st = "";
try {
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if (encoding.equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding);
}
BufferedReader br = new BufferedReader(isr);
try {
String data = "";
while ((data = br.readLine()) != null) {
str.append(data + " ");
}
} catch (Exception e) {
str.append(e.toString());
}
st = str.toString();
} catch (IOException es) {
st = "";
}
return st;
}
/**
* 鏂板緩鐩綍
* @param folderPath 鐩綍
* @return 榪斿洖鐩綍鍒涘緩鍚庣殑璺緞
*/
public String createFolder(String folderPath) {
String txt = folderPath;
try {
java.io.File myFilePath = new java.io.File(txt);
txt = folderPath;
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
message = "鍒涘緩鐩綍鎿嶄綔鍑洪敊";
}
return txt;
}
/**
* 澶氱駭鐩綍鍒涘緩
* @param folderPath 鍑嗗瑕佸湪鏈駭鐩綍涓嬪垱寤烘柊鐩綍鐨勭洰褰曡礬寰?渚嬪 c:myf
* @param paths 鏃犻檺綰х洰褰曞弬鏁幫紝鍚勭駭鐩綍浠ュ崟鏁扮嚎鍖哄垎 渚嬪 a|b|c
* @return 榪斿洖鍒涘緩鏂囦歡鍚庣殑璺緞
*/
public String createFolders(String folderPath, String paths) {
String txts = folderPath;
try {
String txt;
txts = folderPath;
StringTokenizer st = new StringTokenizer(paths, "|");
for (int i = 0; st.hasMoreTokens(); i++) {
txt = st.nextToken().trim();
if (txts.lastIndexOf("/") != -1) {
txts = createFolder(txts + txt);
} else {
txts = createFolder(txts + txt + "/");
}
}
} catch (Exception e) {
message = "鍒涘緩鐩綍鎿嶄綔鍑洪敊錛?;
}
return txts;
}
/**
* 鏂板緩鏂囦歡
* @param filePathAndName 鏂囨湰鏂囦歡瀹屾暣緇濆璺緞鍙?qiáng)鏂囦欢鍚?br> * @param fileContent 鏂囨湰鏂囦歡鍐呭
* @return
*/
public void createFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
myFile.close();
resultFile.close();
} catch (Exception e) {
message = "鍒涘緩鏂囦歡鎿嶄綔鍑洪敊";
}
}
/**
* 鏈夌紪鐮佹柟寮忕殑鏂囦歡鍒涘緩
* @param filePathAndName 鏂囨湰鏂囦歡瀹屾暣緇濆璺緞鍙?qiáng)鏂囦欢鍚?br> * @param fileContent 鏂囨湰鏂囦歡鍐呭
* @param encoding 緙栫爜鏂瑰紡 渚嬪 GBK 鎴栬?UTF-8
* @return
*/
public void createFile(String filePathAndName, String fileContent,
String encoding) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
PrintWriter myFile = new PrintWriter(myFilePath, encoding);
String strContent = fileContent;
myFile.println(strContent);
myFile.close();
} catch (Exception e) {
message = "鍒涘緩鏂囦歡鎿嶄綔鍑洪敊";
}
}
/**
* 鍒犻櫎鏂囦歡
* @param filePathAndName 鏂囨湰鏂囦歡瀹屾暣緇濆璺緞鍙?qiáng)鏂囦欢鍚?br> * @return Boolean 鎴愬姛鍒犻櫎榪斿洖true閬亣寮傚父榪斿洖false
*/
public boolean delFile(String filePathAndName) {
boolean bea = false;
try {
String filePath = filePathAndName;
File myDelFile = new File(filePath);
if(myDelFile.exists()){
myDelFile.delete();
bea = true;
}else{
bea = false;
message = (filePathAndName+"鍒犻櫎鏂囦歡鎿嶄綔鍑洪敊");
}
}
catch (Exception e) {
message = e.toString();
}
return bea;
}
/**
* 鍒犻櫎鏂囦歡澶?br> * @param folderPath 鏂囦歡澶瑰畬鏁寸粷瀵硅礬寰?br> * @return
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); //鍒犻櫎瀹岄噷闈㈡墍鏈夊唴瀹?br> String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //鍒犻櫎絀烘枃浠跺す
} catch (Exception e) {
message = ("鍒犻櫎鏂囦歡澶規(guī)搷浣滃嚭閿?);
}
}
/**
* 鍒犻櫎鎸囧畾鏂囦歡澶逛笅鎵鏈夋枃浠?br> * @param path 鏂囦歡澶瑰畬鏁寸粷瀵硅礬寰?br> * @return
* @return
*/
public boolean delAllFile(String path) {
boolean bea = false;
File file = new File(path);
if (!file.exists()) {
return bea;
}
if (!file.isDirectory()) {
return bea;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//鍏堝垹闄ゆ枃浠跺す閲岄潰鐨勬枃浠?br> delFolder(path + "/" + tempList[i]);//鍐嶅垹闄ょ┖鏂囦歡澶?br> bea = true;
}
}
return bea;
}
/**
* 澶嶅埗鍗曚釜鏂囦歡
* @param oldPathFile 鍑嗗澶嶅埗鐨勬枃浠舵簮
* @param newPathFile 鎷瘋礉鍒版柊緇濆璺緞甯︽枃浠跺悕
* @return
*/
public void copyFile(String oldPathFile, String newPathFile) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPathFile);
if (oldfile.exists()) { //鏂囦歡瀛樺湪鏃?br> InputStream inStream = new FileInputStream(oldPathFile); //璇誨叆鍘熸枃浠?br> FileOutputStream fs = new FileOutputStream(newPathFile);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //瀛楄妭鏁?鏂囦歡澶у皬
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
message = ("澶嶅埗鍗曚釜鏂囦歡鎿嶄綔鍑洪敊");
}
}
/**
* 澶嶅埗鏁翠釜鏂囦歡澶圭殑鍐呭
* @param oldPath 鍑嗗鎷瘋礉鐨勭洰褰?br> * @param newPath 鎸囧畾緇濆璺緞鐨勬柊鐩綍
* @return
*/
public Boolean copyFolder(String oldPath, String newPath) {
try {
new File(newPath).mkdirs(); //濡傛灉鏂囦歡澶逛笉瀛樺湪 鍒欏緩绔嬫柊鏂囦歡澶?br> File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {//濡傛灉鏄瓙鏂囦歡澶?br> copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
return true;
} catch (Exception e) {
message = "澶嶅埗鏁翠釜鏂囦歡澶瑰唴瀹規(guī)搷浣滃嚭閿?;
return false;
}
}
/**
* 縐誨姩鏂囦歡
* @param oldPath
* @param newPath
* @return
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 縐誨姩鐩綍
* @param oldPath
* @param newPath
* @return
*/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
public String getMessage() {
return this.message;
}
public static void main(String[] args) throws Exception {
FileOperate fo = new FileOperate();
String newPath = "e://aaa";
// System.out.println(newPath);
String oldPath = "http://14.1.1.101//bbb";
fo.copyFolder(oldPath, newPath);
// fo.delFolder("c://dola");
// FileOperate.renamePath("d://Audio", "涓嶅浠?);
}
}