基本布局/自上而下的方法
开始与你所知道并将它放入方法。你从名字和他们需要执行的东西开始。不要只考虑逻辑流程,不要考虑实施。
在你的情况,你需要这样的:
public void createHash(File sourceDir, File targetDir)
public String createHash(File file)
public void writeHash(File toFile, String hash)
把它包在main()
你需要在你的类名,以填补在那里。
public static void main(String[] args) {
new YourClass().createHash(new File(args[0]), new File(args[1]));
}
与最外层的方法,实施自上而下
启动,并得到这一权利。你可以从其他的虚拟代码开始。现在
public void createHash(File sourceDir, File targetDir) {
for(File f : sourceDir.listFiles()) {
String hash = createHash(f); //That you almost have
File target = new File(targetDir, f.getName()+".hash");
writeHash(target, hash);
}
}
public String createHash(f) {
return f.getName(); //This is where your existing code will go later
}
public String writeHash(File target, String hash) {
System.out.println("I'd write " + hash + " to File " + file.getName());
}
你的程序应该能够通过源文件夹进行迭代,创建哑散列和打印到System.out哪些文件会写。
瑞风方法
现在一步完成剩下的一步 - 一个方法一次。直到你完成或某件事情中断 - 在这种情况下,你会回来寻求帮助。
public String createHash(File datafile) throws IOException {
//SNIP - YOUR CODE BEGINS
MessageDigest md = MessageDigest.getInstance("SHA1");
FileInputStream fis = new FileInputStream(datafile);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
//convert the byte to hex format
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
//SNAP - YOUR CODE ENDS
}
public void writeFile(File target, String hash) {
try(FileOutputStream fo = new FileOutputStream(target)) {
fo.write(hash.getBytes());
} catch(IOException e) {
System.err.println("No Hash Written for " + target.getName());
}
}
工作实例
import java.io.*;
import java.security.MessageDigest;
public class Checksums {
public static void main(String[] args) {
String sourceDir = "/Users/Jan/Desktop/Folder1";
String targetDir = "/Users/Jan/Desktop/Folder2";
try {
new Checksums().createHash(new File(sourceDir), new File(targetDir));
} catch (Exception e) {
e.printStackTrace();
}
}
private void createHash(File sourceDir, File targetDir) throws Exception {
for (File f : sourceDir.listFiles()) {
String hash = createHash(f); // That you almost have
File target = new File(targetDir, f.getName() + ".hash");
writeHash(target, hash);
}
}
public String createHash(File datafile) throws Exception {
// SNIP - YOUR CODE BEGINS
MessageDigest md = MessageDigest.getInstance("SHA1");
FileInputStream fis = new FileInputStream(datafile);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
// convert the byte to hex format
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
// SNAP - YOUR CODE ENDS
return sb.toString();
}
public void writeHash(File target, String hash) {
try (FileOutputStream fo = new FileOutputStream(target)) {
fo.write(hash.getBytes());
System.out.println("Hash written for " + target.getAbsolutePath());
} catch (IOException e) {
System.err.println("No Hash Written for " + target.getName());
}
}
}
用什么?请问一个问题。 – Jan
我想生成一个文本文件,它已经获得了特定文件夹中所有随机文件的所有散列值。我有一个单一文件的功能,我需要为整个文件夹实现它。 – Raju
因此,对于每个文件,您希望将一个散列写入文件。你已经有** ** **文件的代码。 File.listFiles()可以列出目录中的所有文件。那么你面临的确切问题/错误/问题在哪里? – Jan