I am developing a checksum generator application. I, for now, designed it to give SHA/MD5 output of strings values. I would like to know if you can import files so it can act as an integrity checker too by creating hash values for imported files. Thanks.
Asked
Active
Viewed 1,943 times
0
-
1*"I would like to know if you can import files"* You have my permission. – Andrew Thompson Jun 29 '13 at 11:28
3 Answers
1
are u looking for something like this
FileDialog fd = new FileDialog(parent, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.java");
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null)
System.out.println("file not selected");
else
System.out.println("You chose " + filename);
you can also use JFileChooser

Michael Shrestha
- 2,547
- 19
- 30
0
Using JFileChooser
JFileChooser fileDlg = new JFileChooser();
fileDlg.showOpenDialog(this);
String filename = fileDlg.getSelectedFile().getAbsolutePath();
jTextField1.setText(filename);
FileInputStream fis = new FileInputStream(filename);
byte buffer[] = new byte[fis.available()];
fis.read(buffer);
String message = new String(buffer);
jTextArea1.setText(message);

Ganesh Rengarajan
- 2,006
- 13
- 26
-
See also [`JTextComponent.read(Reader,Object)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#read%28java.io.Reader,%20java.lang.Object%29). – Andrew Thompson Jun 29 '13 at 12:28
0
I suppose you need something like that to deal with files:
import org.apache.commons.codec.binary.Hex;
import javax.swing.*;
import java.io.*;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Application {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
if (JFileChooser.APPROVE_OPTION == result){
File file = chooser.getSelectedFile();
MessageDigest digest = MessageDigest.getInstance("MD5");
try (InputStream is = new FileInputStream(file)) {
DigestInputStream dis = new DigestInputStream(new BufferedInputStream(is), digest);
while (dis.read() != -1){}
}
JOptionPane.showMessageDialog(null, Hex.encodeHexString(digest.digest()));
}
}
}
The point is that files may be quite large, so you should not read full file contents into memory. This implementation streams the file, so it hasn't got large memory footprint. It also demonstrates how to use JFileChooser to acomplish the task.

Jk1
- 11,233
- 9
- 54
- 64
-
-
Reading the file one byte at a time is not very performance friendly. A Java 7-esque approach would be to use `Files.copy(Path, OutputStream)`, which is rather concise yet allows the underlying platform to do certain performance optimizations. – ntoskrnl Jun 29 '13 at 16:42
-
"Reading the file one byte at a time is not very performance friendly" I suppose with buffered stream it should be fine – Jk1 Jun 29 '13 at 16:59
-
It will still cause a performance penalty due to the extra method invocations. Either way, if you have access to Java 7 (which you do, considering the try-with-resources), you should be using Files.copy, which is both simpler and faster than other alternatives. – ntoskrnl Jun 29 '13 at 17:01