/**
* 拷贝的方法类
* @author RUANWENJUN
*
*/
class CopyUtil{
private CopyUtil() {}
/**
* 判断源文件路径是否存在
* @param srcfile 源文件
* @return 如果存在则返回true
*/
public static boolean srcIsExist(String srcfile) {
File file = new File(srcfile);
if(!file.exists()) {
System.out.println("源路径不存在");
return false;
}
if(!file.isFile()) {
System.out.println("源路径不是文件");
return false;
}
if(!file.canExecute()) {
System.out.println("源文件不可操作");
return false;
}
return true;
}
/**
* 判断目标路径下是否存在文件,若不存在则新建一个
* @param goalfile 粘贴文件的路径
* @return true返回成功
*/
public static boolean goalIsExist(String goalfile)throws IOException {
File file = new File(goalfile);
if(file.getParentFile()==null) {
file.mkdirs();
}
file.createNewFile();
return true;
}
/**
* 拷贝文件的方法
* @param srcfile 源文件路径
* @param goalfile 目标文件路径
*/
public static void copy(String srcfile,String goalfile) {
File infile = new File(srcfile);
File outfile = new File(goalfile);
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(infile);
output = new FileOutputStream(outfile);
byte data [] = new byte[1024];
int tem =0;
while((tem =input.read(data))!=-1) {
output.write(data, 0, tem);
}
System.out.println("拷贝完成");
} catch (IOException e) {
System.out.println(e);
}finally {
try {
input.close();
output.close();
}catch(IOException e) {
System.out.println(e);
}
}
}
}
public class Copy {
public static void main(String[] args)throws IOException {
if(args.length!=2) {
System.out.println("参数不对,请输入源文件路径 目标文件路径");
}else {
if(CopyUtil.srcIsExist(args[0])) {
CopyUtil.goalIsExist(args[1]);
CopyUtil.copy(args[0], args[1]);
}else {
System.out.println("源文件有问题");
}
}
}
}