1

我可以知道如何循环,以便为文件(.txt)中的所有单词生成哈希码吗?我已经能够为文件生成单个哈希码。

给定的循环读取并取出文本文档中的单词。但我无法循环生成哈希键。

public static void main(String[] args) throws NoSuchAlgorithmException, IOException { 

  JFileChooser chooser=new  JFileChooser();
  int returnVal = chooser.showOpenDialog(null);
  if (returnVal == JFileChooser.APPROVE_OPTION) {
    File f = chooser.getSelectedFile();
  }
  FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
  DataInputStream din = new DataInputStream(fin);    
  BufferedReader br = new BufferedReader(new InputStreamReader(din)); 

  ArrayList<String> list = new ArrayList<String> ();
  MessageDigest md = MessageDigest.getInstance("MD5");

  String currentLine;
  byte[] buf = new byte[8192];

  int len = 0;
  while ((currentLine = br.readLine()) != null) {
    list.add(currentLine);
    md.update(buf, 0, len);
    System.out.println(currentLine);
  }
  br.close();

  byte[] bytes = md.digest();

  StringBuilder sb = new StringBuilder(2 * bytes.length);
  for (byte b : bytes) {
    sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
    sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
  }
  String hex = sb.toString();

  System.out.println (buf);
  System.out.println(sb);
}
4

3 回答 3

1

On high follow below steps.

  1. Read line by line.
  2. Once you get line split it on \\s+(space).
  3. Now you have all words in array and then iterate it .
  4. For each string (word) call word.hashCode()
于 2012-08-08T10:21:41.363 回答
1

尝试使用这样的标记器:

StreamTokenizer tokenizer = new StreamTokenizer(new FileReader("yourFilePath.txt"));
tokenizer.eolIsSignificant(false);
int token = tokenizer.nextToken();
while (token != StreamTokenizer.TT_EOF) {
    if (token == StreamTokenizer.TT_WORD) {
        System.err.println(tokenizer.sval.hashCode()); // here use any hash method you like
    }
    token = tokenizer.nextToken();
}
于 2012-08-08T10:43:18.763 回答
0

您生成文件中所有的列表,然后您似乎从未使用过这些行。也许您应该通过在空白处分割每一行来生成文件中所有单词的列表;

for (String word : currentLine.split("\\s+")) {
  list.add(word);
}

然后你可以使用这个列表为每个单词创建一个哈希列表;

List<byte[]> hashes = new ArrayList<byte[]>(list.size());
for (String word : list) {
  md.reset();
  hashes.add(md.digest(word));
}
于 2012-08-08T10:27:53.347 回答