因为之前有台电脑硬盘分区的问题,导致复制文件会导致数据丢失的问题,所以使用idea写了java文件,但是那台电脑没有java运行软件,但是有jdk环境,所以就将java文件导出成jar文件,配合xml文件灵活复制文件夹。
好了,直接上java代码:
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
/**
* 动态获取jar包位置,读取同目录下的xml文件
* 读取xml文件中配置的路径,从而实现复制功能
*/
public class T1 {
public static void main(String[] args) {
String jarWholePath = T1.class.getProtectionDomain().getCodeSource().getLocation().getFile();
try {
jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");
} catch (UnsupportedEncodingException e) { System.out.println(e.toString()); }
String jarPath = new File(jarWholePath).getParentFile().getAbsolutePath();//动态获取jar包的位置
long startTime = System.currentTimeMillis(); // 开始执行的时间
String oldPath="";
String newPath="";
try {
File f = new File(jarPath+"/config.xml");//将xml文件和jar包放一起,读取xml文件路径
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
oldPath= doc.getElementsByTagName("oldPath").item(0).getFirstChild().getNodeValue();//读取xml文件中配置的值。
newPath=doc.getElementsByTagName("newPath").item(0).getFirstChild().getNodeValue();
} catch (Exception e) {
e.printStackTrace();
}
File dirNew = new File(newPath);
if(!dirNew.exists())
dirNew.mkdirs();//可以在不存在的目录中创建文件夹
new T1().directory(oldPath, newPath);
System.out.println(oldPath+"复制文件夹成功");
long endTime = System.currentTimeMillis(); // 执行完的时间
long useTime = endTime - startTime; // 执行复制文件夹用的时间(毫秒)
System.out.println("完成复制共用" + useTime + "毫秒");
}
/**
* 复制单个文件
* @param oldPath 要复制的文件名
* @param newPath 目标文件名
*/
public void copyfile(String oldPath, String newPath) {
int hasRead = 0;
File oldFile = new File(oldPath);
File newFile = new File(newPath);
if(newFile.exists()&&newFile.length()==oldFile.length()) return;
if (oldFile.exists()) {
try {
System.out.println("正在传输文件"+oldPath);
FileInputStream fis = new FileInputStream(oldFile);//读入原文件
FileOutputStream fos = new FileOutputStream(newPath);
byte[] buffer = new byte[1024*1024];
while ((hasRead = fis.read(buffer)) != -1) {//当文件没有读到结尾
fos.write(buffer, 0, hasRead);//写文件
}
System.out.println(oldPath+"传输完成");
fis.close();
} catch (Exception e) {
System.out.println("复制单个文件操作出错!");
e.printStackTrace();
}
}
}
/**
*
* @param oldPath 要复制的文件夹路径
* @param newPath 目标文件夹路径
*/
public void directory(String oldPath, String newPath) {
File f1 = new File(oldPath);
File[] files = f1.listFiles();//listFiles能够获取当前文件夹下的所有文件和文件夹
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
File dirNew = new File(newPath + File.separator + files[i].getName());
if(!dirNew.exists())
dirNew.mkdir();//在目标文件夹中创建文件夹
//递归
directory(oldPath + File.separator + files[i].getName(), newPath + File.separator + files[i].getName());
} else {
String filePath = newPath + File.separator + files[i].getName();
copyfile(files[i].getAbsolutePath(), filePath);
}
}
}
}
然后是xml文件,这里比较简单,就随便写写了
<?xml version="1.0" encoding="UTF-8"?>
<value>
<oldPath>D:\ubuntu</oldPath>
<newPath>E:\ubuntu</newPath>
</value>
我们可以通过设置这两个值去改变需要复制的文件以及目的文件路径,不过这个需要的文件需要存在,目的路径可以不存在。
oldPath代表需要复制的文件路径,newPath代表目的路径。
接下来是打包操作,将java文件打包成jar包。
idea->File->Project Structure ->Artifacts
然后点加号选择配置
选择输出jar位置之后build Artifacts 然后build一下
然后我们就可以在刚刚设置的路径上找到生成的jar文件了。
运行可以使用命令行的方式运行,例如这样
我为了偷懒不想每改一次就输一次就写了一个bat脚本,批处理,这样每次直接运行这个脚本就可以了。
我写的脚本如下:
@echo off
java -jar T1.jar
pause
将三个文件放到一个目录然后运行run.bat批处理就可以了。
运行结果如下: