关于通过java基于条件移动序列化文件
我必须制作一个程序,仅当目标文件夹不包含序列化文件时,才能将序列化文件从源文件夹复制到目标文件夹 序列化文件,因此第一个条件是检查我正在复制的文件是否已存在于目标文件夹中 如果存在,则不需要复制,但如果不存在,则复制,因此需要检查文件是否存在 每时每刻 源文件夹是C:\ter\ 目标文件夹是C:\bvg\ 要转换的文件是gfr.ser 我已经制定了以下计划,但仍然没有执行检查。请告知我如何执行此检查 类ScheduledTask扩展了TimerTask{关于通过java基于条件移动序列化文件,java,file-io,Java,File Io,我必须制作一个程序,仅当目标文件夹不包含序列化文件时,才能将序列化文件从源文件夹复制到目标文件夹 序列化文件,因此第一个条件是检查我正在复制的文件是否已存在于目标文件夹中 如果存在,则不需要复制,但如果不存在,则复制,因此需要检查文件是否存在 每时每刻 源文件夹是C:\ter\ 目标文件夹是C:\bvg\ 要转换的文件是gfr.ser 我已经制定了以下计划,但仍然没有执行检查。请告知我如何执行此检查 类ScheduledTask扩展了TimerTask{ public void run() {
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File source = new File("C:\\ter\\");
File target = new File("C:\\avd\\bvg\\");
// Already exists. do not copy
if (target.exists()) {
return;
}
File[] files = source.listFiles();
for (File file : files) {
inStream = new FileInputStream(file);
outStream = new FileOutputStream(target + "/" + file.getName());
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
}
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以像这样使用类的方法
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File source = new File("C:\\ter\\gfr.ser");
File target = new File(" C:\\bvg\\gfr.ser");
if (target.exists()){ // Already exists. do not copy
return;
}
inStream = new FileInputStream(source);
outStream = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
您可以像这样使用类的方法
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File source = new File("C:\\ter\\gfr.ser");
File target = new File(" C:\\bvg\\gfr.ser");
if (target.exists()){ // Already exists. do not copy
return;
}
inStream = new FileInputStream(source);
outStream = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
我想为这个程序制作一个可执行的jar,请告诉我如何做。转到classes文件夹,运行命令
jar-cf MyJar.jar*
exists应该可以工作。这是我们在java中检查文件存在性的唯一方法。我想为这个程序制作一个可执行的jar。请告诉我如何做。转到classes文件夹并运行命令jar-cf MyJar.jar*
exists应该可以工作。这是java中检查文件存在的唯一方法。