Java 在哈希表中存储文件
我正在尝试将HTML转换为文本的文件存储到哈希表中,以便以后检索它们。我不明白如何实施它。请帮帮我。如何将文本文件存储到哈希表中Java 在哈希表中存储文件,java,string,hashtable,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; i
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);
}
}
包哈希表;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.math.biginger;
导入java.util.Scanner;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
公共类哈希映射{
//将表大小设置为最大32,该值用于哈希值的模数。
私有最终静态int表_SIZE=32;
HashEntry[]表;
HashMap(){
table=新的HashEntry[表大小];
对于(int i=0;i st+=“\n”+str.replace(“在hashmap中存储许多大文件不是一个好主意,但是如果您坚持,您可以尝试以下方法:
逐行读取每个文件并将其存储在字符串变量中
为每个字符串变量指定一个自动递增的整数值
将成对的
插入hashmap
瞧!
现在,您有了一个包含所有文件的hashmap。当然,可能会出现异常情况,例如在读取反斜杠等特殊字符时。为什么不将文件存储在文件系统中,然后将文件的路径放在映射中呢?谢谢您的回复。将成对的插入hashmap是什么意思?如何做到这一点hat?我应该使用我的代码的“put”方法吗?是的@Bharadwaj,您需要使用hashmap的“put”方法。还请记住如下声明hashmap:hashmap hmap=new hashmap();您能详细说明您的想法吗?我是说,使用示例代码?将文件写入文件系统。不要将文件放入hashMap,而是将文件的路径放入文件。例如map.put(“file1”,“C:\storage\file1”);但我的put方法参数是(String,Int)。我如何给出存储位置,它是一个字符串,而不是一个整数?将您的HashMap创建为。欢迎使用stackoverflow。请了解如何正确提问。请将您的代码缩减到最低限度以显示您的问题。您的问题到底是什么?转换HTML?将字符串存储在数组中?您的HashEntr是什么y类型?运行此代码时得到了什么?有错误吗?请检查。我找到了一个未找到的文件exception@Heri,我想将转换后的HTML文件存储到哈希表中。我不知道怎么做。