2016-12-05 3 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

このコードを実行するとどうなっていますか?何か間違いはありますか? [ask]を確認してください。 – Fencer04

+0

File not found例外が発生しました – Bharath

答えて

0

これはハッシュマップに多くの大きなファイルを格納するために、このような良いアイデアではありませんが、あなたが主張する場合は、次の試みることができる:

  1. 線で各ファイルの行を読み取り、それを保存します文字列変数
  2. 各文字列変数に自動的にインクリメントされた整数値を割り当てます。
  3. ハッシュマップに<Integer, String>のペアを挿入します。

Voila! すべてのファイルを含むハッシュマップが作成されました。バックスラッシュなどの特殊文字を読むときなど、例外が発生する可能性があります。

+0

ありがとうございます。ハッシュマップにペアを挿入するとどういう意味ですか?どうやってするか?私のコードの "put"メソッドを使うか? – Bharath

+0

はい@Bharadwaj、あなたはハッシュマップの "put"メソッドを使う必要があります。 HashMap hmap = new HashMap <>();このようにハッシュマップを宣言することも覚えておいてください。 –

0

ファイルをfilesytemに保存せずに、ファイルのパスをマップに保存してみませんか?

+0

あなたはサンプルコードを使ってあなたのアイデアを具体化してください。 – Bharath

+0

ファイルをファイルシステムに書き込みます。ファイルをhashMapに入れるのではなく、パスをファイルに置きます。 map.put( "file1"、 "C:\ storage \ file1")と同様です。 –

+0

しかし、私のputメソッドのパラメータは(String、Int)です。整数の代わりに文字列の格納場所を与えるにはどうすればよいですか? – Bharath

関連する問題