java - 在哈希表中存储文件

标签 java string hashtable

我正在尝试将 HTML 到文本转换的文件存储到哈希表中,以便稍后检索它们。我不明白如何实现它。请帮我。如何将文本文件存储到哈希表中?

package hashTable;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class HashMap {
  // Setting table size to a max of 32, value used to modulus for hash value.
  private final static int TABLE_SIZE = 32;

  HashEntry[] table;

  HashMap() {
        table = new HashEntry[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = null;
  }

  /* function to retrieve value from the table according to key */
  public int get(String key) {
        int hash = new BigInteger(toAscii(key)).mod(new BigInteger(((Integer)TABLE_SIZE).toString())).intValue();
        while (table[hash] != null && table[hash].getKey() != key)
              hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] == null)
              return -1;
        else
              return table[hash].getValue();
  }

  /* function to add value to the table */
  public void put(String key, int value) {
        //creating hash code using key value given as a string
        int hash = new BigInteger(toAscii(key)).mod(new BigInteger(((Integer)TABLE_SIZE).toString())).intValue();
        while (table[hash] != null && table[hash].getKey() != key)
              hash = (hash + 1) % TABLE_SIZE;
        table[hash] = new HashEntry(key, value);
  }

  /* value to create the Hash code from he name entered, basically converting name to ASCII */
  public static String toAscii(String s){
      StringBuilder sb = new StringBuilder();
      long asciiInt;
      // loop through all values in the string, including blanks
      for (int i = 0; i < s.length(); i++){
          //getting Ascii value of character and adding it to the string.
          char c = s.charAt(i);
          asciiInt = (int)c; 
          sb.append(asciiInt);
      }
      return String.valueOf(sb);
     }
      public void HtmltoText(String fn){
      try{
         String uri="C:/Users/Bharadwaj/Downloads/W3C Web Pages";
         BufferedReader in = new BufferedReader(new FileReader(uri));
         String st=new String();
         String str;
         while((str=in.readLine())!=null){
             st += "\n" + str.replace("<br", "\n<br");
                         }        
         Document s=Jsoup.parse(st);
        // System.out.println(s1);
         String text=s.text();
        // System.out.println(filename.substring(0,filename.length()-4));   
         String txtpath="C:/Users/Bharadwaj/Downloads/W3C Web Pages/Text";
         System.out.println(text);
         String newname=txtpath+fn.substring(0,(fn.length()-4))+".txt";          
         BufferedWriter writerTxt = new BufferedWriter(new FileWriter(newname));
         writerTxt.write(text);
         writerTxt.close();                      
      }catch(Exception e){
          e.printStackTrace();
      }     
     }  

  public static void main(String[]args) throws IOException{
    HashMap entry = new HashMap();
    String uri="C:/Users/Bharadwaj/Downloads/W3C Web Pages";
    File fil=new File(uri);
    System.out.println(fil);
    entry.HtmltoText(uri);       
  }
}

最佳答案

在 HashMap 中存储许多大文件并不是一个好主意,但如果您坚持,可以尝试以下操作:

  1. 逐行读取每个文件并将其存储在字符串变量中
  2. 为每个字符串变量分配一个自动递增的整数值
  3. 插入<Integer, String>对进入 HashMap 。

瞧! 现在您有了一个包含所有文件的 HashMap 。当然可能会发生异常,例如在读取反斜杠等特殊字符时......

关于java - 在哈希表中存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980236/

相关文章:

java - Java 中的无效转义序列

c# - 有没有一种简单的方法可以按字母顺序对字符串中的字符进行排序

c++ - 我的哈希表比二进制搜索慢

C++:为什么我的 hash_map 给我一个像 map 一样的有序结果?

c++ - ‘<’ 标记之前预期的构造函数、析构函数或类型转换

java - 组合算法

java - Mapreduce 写入名称中有空格的目录

c++ - "error: subscripted value is not an array, pointer, or vector"我正在使用一个字符串

java - 如何在这个例子中使用 hashmap 中的可变键?

windows - 如何防止java.exe安装在Windows的system32中?