使用java 如何判断两个文件是否相同呢?
我的做法是
(1)先比较两个文件内容的长度;
(2)在长度相同的情况下,再比较两个文件的MD5值。
【create md5】按钮用于生成source file的文件内容长度和MD5值。
运行主类:CheckSameApp package com.hw.main; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import com.common.util.SystemUtil; import com.swing.dialog.DialogUtil; import com.swing.messagebox.GUIUtil23; public class CheckSameApp extends JFrame { private static final long serialVersionUID = 1644076682819874235L; private JTextField sourceFileTF; private JButton browserSourceBtn; private JTextField targetFileTF; private JButton createmd5Button = null; private JButton checkmd5Btn = null; private JButton browserTargetBtn = null; /*** * MD5 of last file */ private String result = null; private long size_of_file = 0; private JButton compareBtn = null; private File srcfile = null; private File targfile = null; protected static String MESG_DIFF = "[failed:] they are different"; protected static String MESG_SAME = "[successfully:] they are same completely"; public static void main(String[] args) { CheckSameApp app = new CheckSameApp(); app.launchFrame(); } public void launchFrame() { this.setTitle("Compare two files by MD5"); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(700, 200); Dimension framesize = this.getSize(); int x = (int) screensize.getWidth() / 2 - (int) framesize.getWidth() / 2; int y = (int) screensize.getHeight() / 2 - (int) framesize.getHeight() / 2; this.setLocation(x, y); Container c = this.getContentPane(); layout(c); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /*** * setting menu */ public void layout(Container c) { // setMenu2(); JPanel mainPane = new JPanel(); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[] { 20, 80/*between source file and text field*/, 300, 60, 0 }; gridBagLayout.rowHeights = new int[] { 17, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0 }; gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE }; gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE }; mainPane.setLayout(gridBagLayout); JLabel ftpServerLb = new JLabel("source file"); GridBagConstraints gbc_setConnectionsLabel = new GridBagConstraints(); gbc_setConnectionsLabel.anchor = GridBagConstraints.WEST; gbc_setConnectionsLabel.insets = new Insets(0, 0, 5, 5); gbc_setConnectionsLabel.gridx = 1; gbc_setConnectionsLabel.gridy = 1; mainPane.add(ftpServerLb, gbc_setConnectionsLabel); // dialog.add(ftpserverTF); sourceFileTF = new JTextField(); if (!SystemUtil.isWindows) { sourceFileTF.setText("/home/whuang2/bin/ab.txt"); } else { sourceFileTF.setText(""); } GridBagConstraints gbc_connectionsTF = new GridBagConstraints(); gbc_connectionsTF.fill = GridBagConstraints.HORIZONTAL; gbc_connectionsTF.insets = new Insets(0, 0, 5, 5); gbc_connectionsTF.gridx = 2; gbc_connectionsTF.gridy = 1; mainPane.add(sourceFileTF, gbc_connectionsTF); browserSourceBtn = new JButton("browser source"); GridBagConstraints gbc_browserSourceBtn = new GridBagConstraints(); gbc_browserSourceBtn.fill = GridBagConstraints.EAST; gbc_browserSourceBtn.insets = new Insets(0, 0, 5, 5); gbc_browserSourceBtn.gridx = 3; gbc_browserSourceBtn.gridy = 1; mainPane.add(browserSourceBtn, gbc_browserSourceBtn); browserSourceBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println("source"); boolean isSuccess = DialogUtil.browser3(sourceFileTF, JFileChooser.FILES_ONLY, CheckSameApp.this); // if (isSuccess) // { // targetFileTF.setText(SystemUtil.getParentDir(sourceFileTF // .getText())); // } } }); JPanel buttonPane = new JPanel(); // buttonPane.setBackground(Color.red); GridBagConstraints gbc_buttonPane = new GridBagConstraints(); gbc_buttonPane.fill = GridBagConstraints.HORIZONTAL; gbc_buttonPane.insets = new Insets(0, 0, 5, 5); gbc_buttonPane.gridx = 2; gbc_buttonPane.gridy = 2; mainPane.add(buttonPane, gbc_buttonPane); createmd5Button = new JButton("create md5"); checkmd5Btn = new JButton("check"); checkmd5Btn.setEnabled(false); buttonPane.add(createmd5Button); buttonPane.add(checkmd5Btn); createmd5Button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!validate3(true)) { return; } sourceFileTF.setEditable(false); new Thread(new Runnable() { @Override public void run() { createmd5Button.setEnabled(false); size_of_file = srcfile.length(); result = SystemUtil.getFileMD5(srcfile); checkmd5Btn.setEnabled(true); createmd5Button.setEnabled(true); } }).start(); } }); checkmd5Btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // if (!validate3(true)) // { // return; // } long size_of_file2 = srcfile.length(); if (size_of_file2 != size_of_file) { System.out.println("by size"); GUIUtil23.errorDialog(MESG_DIFF); return; } new Thread(new Runnable() { @Override public void run() { checkmd5Btn.setEnabled(false); String result2; sourceFileTF.setEditable(true); result2 = SystemUtil.getFileMD5(srcfile); boolean isSame2=isSame(result2, result); checkmd5Btn.setEnabled(true); if (isSame2) { GUIUtil23.infoDialog(MESG_SAME); } else { GUIUtil23.errorDialog(MESG_DIFF); } } }).start(); } }); JLabel targetLabel = new JLabel("target file"); GridBagConstraints gbc_driveClassLabel = new GridBagConstraints(); gbc_driveClassLabel.anchor = GridBagConstraints.WEST; gbc_driveClassLabel.insets = new Insets(0, 0, 5, 5); gbc_driveClassLabel.gridx = 1; gbc_driveClassLabel.gridy = 3; mainPane.add(targetLabel, gbc_driveClassLabel); targetFileTF = new JTextField(); if (!SystemUtil.isWindows) { targetFileTF.setText("/home/whuang2/bin/wh_dos2unix"); } GridBagConstraints gbc_driveClassTF = new GridBagConstraints(); gbc_driveClassTF.insets = new Insets(0, 0, 5, 5); gbc_driveClassTF.fill = GridBagConstraints.HORIZONTAL; gbc_driveClassTF.gridx = 2; gbc_driveClassTF.gridy = 3; mainPane.add(targetFileTF, gbc_driveClassTF); targetFileTF.setColumns(10); browserTargetBtn = new JButton("browser target"); GridBagConstraints gbc_browserTargetBtn = new GridBagConstraints(); gbc_browserTargetBtn.fill = GridBagConstraints.EAST; gbc_browserTargetBtn.insets = new Insets(0, 0, 5, 5); gbc_browserTargetBtn.gridx = 3; gbc_browserTargetBtn.gridy = 3; mainPane.add(browserTargetBtn, gbc_browserTargetBtn); browserTargetBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println("target"); DialogUtil.browser3(targetFileTF, JFileChooser.FILES_AND_DIRECTORIES, CheckSameApp.this); } }); compareBtn = new JButton("compare"); GridBagConstraints gbc_runBtn = new GridBagConstraints(); gbc_runBtn.fill = GridBagConstraints.EAST; gbc_runBtn.insets = new Insets(0, 0, 5, 5); gbc_runBtn.gridx = 4; gbc_runBtn.gridy = 3; mainPane.add(compareBtn, gbc_runBtn); compareBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!validate3(false)) { return; } long size_of_targfile = targfile.length(); long size_of_srcfile = srcfile.length(); if (size_of_targfile != size_of_srcfile) { System.out.println("by size"); GUIUtil23.errorDialog(MESG_DIFF); return; } new Thread(new Runnable() { @Override public void run() { compareBtn.setEnabled(false); String result_source = SystemUtil.getFileMD5(srcfile); String result_target; result_target = SystemUtil.getFileMD5(targfile); boolean isSame2=isSame(result_source, result_target); compareBtn.setEnabled(true); if (isSame2) { GUIUtil23.infoDialog(MESG_SAME); } else { GUIUtil23.errorDialog(MESG_DIFF); } } }).start(); } }); c.add(mainPane, BorderLayout.CENTER); // System.out.println(c.getLayout()); //new JScrollPane(text) } // private String create_md5(String filePath) // { // try // { // return create_md5(filePath, null); // } // catch (IOException e) // { // e.printStackTrace(); // } // return null; // } /*** * * @param result_source : such as b79898bb7907648871745cd5422c79ce /home/whuang2/bin/ab.txt * @param result_target * @return */ private boolean isSame(String result_source, String result_target) { if (result_source == null || result_target == null) { return false; } return (result_source.split("[ \t]")[0].equals(result_target .split("[ \t]")[0])); } private boolean validate3(boolean isSelf) { String sourceFile = sourceFileTF.getText(); String targetFile_dir = targetFileTF.getText(); if (sourceFile == null || sourceFile.equals("")) { GUIUtil23 .warningDialog("source file can not be empty,please select again !"); sourceFileTF.requestFocus();//focus return false; } if (!isSelf) { if (targetFile_dir == null || targetFile_dir.equals("")) { GUIUtil23 .warningDialog("target file can not be empty,please select again !"); targetFileTF.requestFocus();//focus return false; } } // System.out.println("source file:" + sourceFile); // System.out.println("target file:" + targetFile_dir); srcfile = new File(sourceFile); if (!srcfile.exists()) { GUIUtil23 .warningDialog("source file does not exist,please select again!"); sourceFileTF.requestFocus();//focus sourceFileTF.selectAll(); return false; } if (srcfile.exists() && srcfile.isDirectory()) { GUIUtil23 .warningDialog("source file can not be directory,please select again!"); sourceFileTF.requestFocus();//focus sourceFileTF.selectAll(); return false; } if (!isSelf) { { targfile = new File(targetFile_dir); if (!targfile.exists()) { GUIUtil23 .warningDialog("target file does not exist,please select again!"); targetFileTF.requestFocus();//focus targetFileTF.selectAll(); return false; } if (targfile.exists() && targfile.isDirectory()) { GUIUtil23 .warningDialog("target file can not be directory,please select again!"); targetFileTF.requestFocus();//focus targetFileTF.selectAll(); return false; } } } return true; } // public static void main2(String[] args) // { // String sourceFile = "/home/whuag2/workspace/io0007-find_progess/src/com/cmd/dos/hw/util/CMDUtil.java"; // md5(sourceFile); // } }swingwork类:CheckSameSwingWorker
package com.hw.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import javax.swing.SwingWorker; import com.common.util.MyProcess; public class CheckSameSwingWorker extends SwingWorker<Boolean, Character> { private BufferedReader br_right = null; private BufferedReader br_error = null; private MyProcess myprocess = null; private char word = ' '; private int tmp = 0; private boolean isPrintVerbose = false; private StringBuffer stringbuf = new StringBuffer(); public CheckSameSwingWorker(MyProcess myprocess, BufferedReader br) { this.br_right = br; this.myprocess = myprocess; } public CheckSameSwingWorker(MyProcess myprocess) { this.myprocess = myprocess; br_right = new BufferedReader(new InputStreamReader( myprocess.getInputStream()), 4096); br_error = new BufferedReader(new InputStreamReader( myprocess.getErrorStream()), 4096); } @Override protected Boolean doInBackground() throws Exception { while ((tmp = br_right.read()) != -1) { word = (char) tmp; publish(word); } while ((tmp = br_error.read()) != -1) { word = (char) tmp; publish(word); } if (isPrintVerbose) { System.out.println("doInBackground() over"); } return true; } @Override protected void process(List<Character> chunks) { for (char temp : chunks) { { // System.out.print(temp); this.stringbuf.append(temp); } } } public StringBuffer getStringbuf() { return stringbuf; } /*** * main thread can't execute next command(below waitFor()) * until done() is executed */ @Override protected void done() { if (isPrintVerbose) { System.out.println("done() is finish"); } try { br_right.close(); } catch (IOException e) { e.printStackTrace(); } this.myprocess.stopLoop(); } }
依赖的jar是io0007-find_progess.jar
在附件中。
相关推荐
5G通信行业、网络优化、通信工程建设资料。
29-【智慧城市与政府治理分会场】10亿大数据助推都市治理-30页.pdf
ABB IRC5 Compact 机器人产品手册
5G通信行业、网络优化、通信工程建设资料
5G通信行业、网络优化、通信工程建设资料。
5G通信、网络优化与通信建设
网络技术和计算机技术发展至今,已经拥有了深厚的理论基础,并在现实中进行了充分运用,尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代,所以对于信息的宣传和管理就很关键。系统化是必要的,设计网上系统不仅会节约人力和管理成本,还会安全保存庞大的数据量,对于信息的维护和检索也不需要花费很多时间,非常的便利。 网上系统是在MySQL中建立数据表保存信息,运用SpringBoot框架和Java语言编写。并按照软件设计开发流程进行设计实现。系统具备友好性且功能完善。 网上系统在让售信息规范化的同时,也能及时通过数据输入的有效性规则检测出错误数据,让数据的录入达到准确性的目的,进而提升数据的可靠性,让系统数据的错误率降至最低。 关键词:vue;MySQL;SpringBoot框架 【引流】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes
5G通信行业、网络优化、通信工程建设资料
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
Gif动画录制软件是一款方便好用的小软件,使用此工具,您可以记录屏幕的选定区域,网络摄像头的实时提要或草图板上的实时图形。之后,您可以编辑动画并将其另存为gif,apng,视频,psd或png图像。
协同供应链集成(Collaborative Supply Chain Integration,CSCD是数字化转型解决方案中的重要一环,旨在通过技术手段实现供应链各环节的无缝对接与高效协作。V70版本作为该解决方案的一个升级版,可能包含了更加先进的功能和工具,以支持企业在复杂的商业环境中保持竞争力。这个资料包“协同供应链集成详细功能介绍V70.rar”很可能提供了对V70版本功能的深入剖析,包括了如何利用最新的数字化工具和技术来优化库存管理、订单处理、物流跟踪、供应商管理以及客户关系管理等关键供应链过程。它可能详细介绍了实时数据共享、自动化流程、预测分析、风险管理和智能决策支持系统等创新功能,这些功能有助于减少供应链中断,提高透明度,降低运营成本,并增强整个供应链网络的响应能力。通过这个资料,企业可以获得实施协同供应链集成的全面指导,了解如何通过集成的信息系统和平台,实现数据的一致性和准确性,以及如何通过合作伙伴之间的紧密协作,提升整个供应链的效率和灵活性。这对于追求数字化转型的企业来说,是一个宝贵的资源,因为它不仅提供了理论框架,还可能包含了实际案例研究、最佳实践和实施策略,帮助企业
英飞凌官方ADS库1.9.20版
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
5G通信行业、网络优化、通信工程建设资料。
5G通信行业、网络优化、通信工程建设资料
5G通信行业、网络优化、通信工程建设资料
jump(1).mobileprovision
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
Exam2024SpringA.ipynb