2016-12-05 45 views
0

我想将HTML到文本转换后的文件存储到哈希表中以便稍后检索它们。我不理解如何实施它。请帮帮我。我如何将文本文件存储到哈希表中?在HashTable中存储文件

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);  
    } 
} 
+0

欢迎使用stackoverflow。请参考如何正确提问。请将您的代码降到最低,以显示您的问题。你的问题到底是什么?转换HTML?在数组中存储一个字符串?什么是你的HashEntry类型? – Heri

+0

当你运行这段代码时,你会得到什么?有没有错误?请查看[问]。 – Fencer04

+0

我收到了文件未找到异常 – Bharath

回答

0

这是不是这样存储在一个HashMap许多大型文件是个好主意,但如果你坚持,你可以尝试以下方法:

  1. 逐行读取每个文件行并将其存储在一个字符串变量
  2. 为每个字符串变量指定一个自动递增的整数值
  3. <Integer, String>对的对插入到散列映射中。

瞧! 现在你有一个包含所有文件的散列表。当然,例外情况可能会发生,例如在阅读特殊字符(如反斜杠等)时...

+0

谢谢你的回复。通过在hashmap中插入成对的字符意味着什么?怎么做?我应该使用我的代码的“put”方法吗? – Bharath

+0

是@Bharadwaj,你需要使用hashmap的“put”方法。并且要记住像这样声明hashmap:HashMap hmap = new HashMap <>(); –

0

为什么不将文件存储在filesytem中,只是将文件的路径放在地图中?

+0

请问您是否可以用示例代码来详细说明您的想法? – Bharath

+0

将文件写入文件系统。而不是把文件放在hashMap中,把路径放到文件中。像map.put(“file1”,“C:\ storage \ file1”); –

+0

但我的put方法参数是(String,Int)。我怎样才能给一个字符串的存储位置,在一个整数的地方? – Bharath