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

에 오신 것을 환영합니다. 질문을 올바르게하는 방법에 대해 살펴보십시오. 문제를 나타 내기 위해 코드를 최소한으로 끓여주십시오. 당신의 문제는 정확히 무엇입니까? HTML 변환? 배열에 문자열 저장? 당신의 HashEntry 타입은 무엇입니까? – Heri

+0

이 코드를 실행할 때 무엇을 얻고 있습니까? 어떤 오류가 있습니까? [ask]를 검토하십시오. – Fencer04

+0

파일을 찾을 수 없습니다. – Bharath

답변

0

이것은 해시 맵에 많은 큰 파일을 저장하는 그런 좋은 생각이 아니다, 그러나 당신이 주장하는 경우 다음과 같은 시도 할 수 :

  1. 라인으로 각 파일 라인을 읽고에 저장을 문자열 변수
  2. 각 문자열 변수에 자동으로 증가 된 정수 값을 할당하십시오.
  3. 해시 맵에 <Integer, String> 쌍을 삽입하십시오.

Voila! 이제 모든 파일을 포함하는 해시 맵을 만들었습니다. 백 슬래시 등과 같은 특수 문자를 읽을 때 예외가 발생할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. hashmap에 쌍을 삽입하면 무슨 뜻입니까? 그렇게하는 방법? 내 코드의 "put"메서드를 사용해야합니까? – Bharath

+0

예 @Bharadwaj, 해시 맵의 "put"메소드를 사용해야합니다. 또한 다음과 같이 해시 맵을 선언해야합니다. HashMap hmap = new HashMap <>(); –

0

파일을 파일에 저장하지 않고 파일의 경로를 맵에 저장하는 것이 어떻습니까?

+0

샘플 코드를 사용하여 아이디어를 상세히 설명해 주시겠습니까? – Bharath

+0

파일을 파일 시스템에 씁니다. hashMap에 파일을 저장하는 대신 경로를 파일에 저장하십시오. map.put ("file1", "C : \ storage \ file1"); –

+0

하지만 내 put 메서드 매개 변수는 (String, Int)입니다. 어떻게하면 정수 위치에 문자열 인 저장소 위치를 제공 할 수 있습니까? – Bharath