📄 movepath.java
字号:
/*
* 创建日期 2005-10-8
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package net.nutch.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class MovePath {
public static final Logger LOG = Logger.getLogger("segment");
public static boolean copyFile(File src, File dest) {
try{
if (!src.exists()){
LOG.warn("Source File:"+src.getName()+" is not exist!");
return false;
}
if (dest.exists())
dest.delete();
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] line = new byte[102400];
int n = 0;
while((n=in.read(line))>0){
out.write(line,0,n);
}
in.close();
out.close();
}catch(Exception e){
LOG.error("Copy file "+ src.getAbsolutePath() + " to " + dest.getAbsolutePath() + " Error!");
LOG.error(e.toString());
return false;
}
return true;
}
public static boolean copyPath(File src,File dest)throws IOException{
//System.out.println("move from "+ src.getAbsolutePath() + " to "+ dest.getAbsolutePath());
if (src.isFile())
return copyFile(src,new File(dest,src.getName()));
File destPath = new File(dest,src.getName());
if (!destPath.exists()){
destPath.mkdir();
}
File[] subFiles = src.listFiles();
//System.out.println("++++++++++"+subFiles.length);
for( int i=0; i<subFiles.length; i++){
copyPath(subFiles[i],destPath);
}
return true;
}
public static boolean delete(File f) throws IOException {
if (f.isFile()) {
return f.delete();
} else return fullyDelete(f);
}
private static boolean fullyDelete(File dir) throws IOException {
File contents[] = dir.listFiles();
if (contents != null) {
for (int i = 0; i < contents.length; i++) {
if (contents[i].isFile()) {
if (! contents[i].delete()) {
return false;
}
} else {
if (! fullyDelete(contents[i])) {
return false;
}
}
}
}
return dir.delete();
}
public static boolean movePath(File src, File dest) throws IOException {
return movePath(src, dest, true);
}
public static boolean movePath(File src,File dest, boolean del) throws IOException{
try{
int n= 0;
while( !copyPath(src,dest)){
Thread.sleep(10000);
if ( n++ > 3 ){
LOG.error("copyPath error "+ n + " times!!!!");
return false;
}
}
}catch(Exception e){
LOG.error("MovePath error:"+e.toString());
return false;
}
if (del)
delete(src);
return true;
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -