基本佈局/自上而下的方法
開始與你所知道並將它放入方法。你從名字和他們需要執行的東西開始。不要只考慮邏輯流程,不要考慮實施。
在你的情況,你需要這樣的:
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