前言:
大四实习,第一个月忙着给公司处理杂活,终于这两天把标准文档翻译完了,用户说明书什么的也都写好了,领导派下来了一些小杂活,当作练练手吧。
正式内容:
这个小程序主要是实现了如下功能:
注:data下存储静态常量,方便修改程序。
package tsc.data;
public class Data {
//源bin文件所在绝对路径
public static final String SOURCE = "D:/ceshi/Source/source.bin";
//需要遍历的路径
public static final String PATH = "D:/ceshi/Test";
//文档要对比的字节数
public static final int BYTE = 64;
}
1.在某个文件夹下进行遍历,去寻找后缀名为.bin的文件
package tsc.function;
import java.io.File;
import tsc.data.Data;
/**
* 遍历功能
* @author qiuyue
*
*/
public class TraverseFolder {
public static void traverseFolder(String path) {
File file = new File(path);
if(file.exists()) {
//如果路径存在,则返回此路径目录下所有文件和目录的绝对路径,返回的是File数组
File[] files = file.listFiles();
if(files.length == 0) {
return;
} else {
for(File file2 : files) {
if(file2.isDirectory()) {
//如果遍历中遇到了文件夹,则对此文件夹再进行遍历
traverseFolder(file2.getAbsolutePath());
} else {
if(file2.getName().endsWith("bin")) {
//如果遇到了.bin文件,则调用CompareFile进行比较
CompareFile.compareFile(file2.getAbsolutePath(),Data.SOURCE);
}
}
}
}
} else {
return;
}
}
}
2.如果找到了.bin文件,则将其与另外一个.bin文件进行比较(IO流)
此处先比较了两文件前64个字节,如果一样证明为同一类文件,可以复制,下面再比较长度,长度不同再比较内容,只要有一项不同就进行复制。
package tsc.function;
import java.io.FileInputStream;
import java.io.IOException;
import tsc.data.Data;
/**
* 文件比较功能
* @author qiuyue
*
*/
public class CompareFile {
public static boolean compareFile(String file,String source) {
FileInputStream f1 = null;
FileInputStream f2 = null;
try {
f1 = new FileInputStream(file);
f2 = new FileInputStream(source);
byte[] data1 = new byte[Data.BYTE];
byte[] data2 = new byte[Data.BYTE];
int len1 = f1.available();
int len2 = f2.available();
f1.read(data1);
f2.read(data2);
for(int i=0;i<Data.BYTE;i++) {
if(data1[i] != data2[i]) {
System.out.println("两个文件不是同一类文件");
return false;
}
}
System.out.println("两个文件是同一类文件");
if(len1 == len2){
byte[] data3 = new byte[len1];
byte[] data4 = new byte[len2];
f1.read(data3);
f2.read(data4);
for(int i=0;i<len1;i++){
if(data3[i] != data4[i]){
System.out.println("两个文件长度相同,内容不相同,开始复制");
CopyFile.copyFile(file,source);
return true;
}
}
System.out.println("两个文件完全相同,不再复制");
return false;
}
System.out.println("两个文件长度不相同,开始复制");
CopyFile.copyFile(file,source);
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if(f1 != null) {
try {
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(f2 != null) {
try {
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}
3.比较完成,符合的就将其复制到SOURCE
package tsc.function;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件复制功能
* @author qiuyue
*
*/
public class CopyFile {
public static boolean copyFile(String file,String source) {
int byteread = 0;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(source);
byte[] buffer = new byte[2048];
while((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
}
System.out.println("复制成功");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
附:1.定时器
package tsc.function;
import java.util.TimerTask;
import tsc.data.Data;
/**
* 定时器
* @author qiuyue
*
*/
public class MyTask {
public static class myTask extends TimerTask{
public void run() {
TraverseFolder.traverseFolder(Data.PATH);
}
}
}
2.前端小窗口
package tsc.ui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.*;
import tsc.function.MyTask.myTask;
public class MyFrame extends JFrame{
private static final long serialVersionUID = 371204056893587903L;
public MyFrame() {
this.setTitle("bin文件比较程序");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 150);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setLayout(new GridLayout(1, 2));
JButton start = new JButton("开始");
JButton exit = new JButton("退出");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Timer timer = new Timer();
myTask mytask = new myTask();
timer.scheduleAtFixedRate(mytask, 0, 5000);
setExtendedState(JFrame.ICONIFIED);
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
getContentPane().add(start);
getContentPane().add(exit);
}
}
3.主程序
package tsc.main;
import tsc.ui.MyFrame;
public class Main {
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
4.引入的jar包
就这么个简单的小程序,给新手一个借鉴。(虽然我也是超级萌新)