Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将多个文件读入哈希表数组并获取相应的文件名_Java - Fatal编程技术网

Java 如何将多个文件读入哈希表数组并获取相应的文件名

Java 如何将多个文件读入哈希表数组并获取相应的文件名,java,Java,我想读取一个文件夹中的所有文件,每个文件的内容都将被读取到一个哈希表中。然后我需要将文本文件中的每个单词与这个哈希表中的每个单词进行比较。如果该单词与该哈希表中的任何单词匹配,则变量将以创建该哈希表的相应文件名命名。 现在我有两个困难: 1.如何为文件夹中的每个文件创建哈希表列表 2.在该哈希表中查找单词时如何命名变量 Hashtable HashTableName; public String namebymatching; // compare the spannedText

我想读取一个文件夹中的所有文件,每个文件的内容都将被读取到一个哈希表中。然后我需要将文本文件中的每个单词与这个哈希表中的每个单词进行比较。如果该单词与该哈希表中的任何单词匹配,则变量将以创建该哈希表的相应文件名命名。 现在我有两个困难:

1.如何为文件夹中的每个文件创建哈希表列表

2.在该哈希表中查找单词时如何命名变量

 Hashtable HashTableName;

  public String namebymatching;


  // compare the spannedText for words in each dictionary in folder
  public OneExtractor() throws IOException {
    super();
    // location
    HashTableName = new Hashtable();


    FilenameFilter ff = new OnlyExt("txt");
    File folder = new File("/Folder Path/");
    File[] files = folder.listFiles(ff);
    Map<String, String> map = new LinkedHashMap<String, String>();
    for (int i = 0; i < files.length; i++) {
      FileReader fr = new FileReader(files[i].getPath());
      BufferedReader reader = new BufferedReader(fr);
      String st = "", str = " ";
      while ((st = reader.readLine()) != null) {
        str += st + " ";
      }
      map.put(files[i].getName(), str);
    }
    Set set = map.entrySet();
    Iterator i = set.iterator();
    while (i.hasNext()) {
      Map.Entry me = (Map.Entry) i.next();

      BufferedReader br = null;
      try {
        br = new BufferedReader(new FileReader(
            "/Folder Path"+me.getKey()));
      } catch (FileNotFoundException ex) {
        Logger.getLogger(PersonExtractor.class.getName()).log(Level.SEVERE, null, ex);
      }
      try {
        String line = br.readLine();
        HashTableName.put(line.toLowerCase(), 1);
        while (line != null) {
          line = br.readLine();
          if (!line.isEmpty())
            HashTableName.put(line.toLowerCase(), 1);
        }
      } catch (Exception ex) {

      } finally {
        try {
          br.close();
        } catch (IOException ex) {
          Logger.getLogger(PersonExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

  }

  private boolean isHashTableName(String s) {
    return HashTableName.containsKey(s.toLowerCase());
  }

  ///Extension  
  public static class OnlyExt implements FilenameFilter {
    String ext;

    public OnlyExt(String ext) {
      this.ext = "." + ext;
    }

    public boolean accept(File dir, String name) {
      return name.endsWith(ext);
    }
  }
// Find word match :
   String word = //some function here to extract word;

    namebymatching = "NOT" + filename; //filename should be here
    if (isHashTableName(spannedText))
      namebymatching = "ISPARTOF" +filename;//filename should be here
我尝试了这段代码,它适用于1个文件,1个哈希表

 Hashtable HashTableName;

  public String namebymatching;


  // compare the spannedText for words in each dictionary in folder
  public OneExtractor() throws IOException {
    super();
    // location
    HashTableName = new Hashtable();


    FilenameFilter ff = new OnlyExt("txt");
    File folder = new File("/Folder Path/");
    File[] files = folder.listFiles(ff);
    Map<String, String> map = new LinkedHashMap<String, String>();
    for (int i = 0; i < files.length; i++) {
      FileReader fr = new FileReader(files[i].getPath());
      BufferedReader reader = new BufferedReader(fr);
      String st = "", str = " ";
      while ((st = reader.readLine()) != null) {
        str += st + " ";
      }
      map.put(files[i].getName(), str);
    }
    Set set = map.entrySet();
    Iterator i = set.iterator();
    while (i.hasNext()) {
      Map.Entry me = (Map.Entry) i.next();

      BufferedReader br = null;
      try {
        br = new BufferedReader(new FileReader(
            "/Folder Path"+me.getKey()));
      } catch (FileNotFoundException ex) {
        Logger.getLogger(PersonExtractor.class.getName()).log(Level.SEVERE, null, ex);
      }
      try {
        String line = br.readLine();
        HashTableName.put(line.toLowerCase(), 1);
        while (line != null) {
          line = br.readLine();
          if (!line.isEmpty())
            HashTableName.put(line.toLowerCase(), 1);
        }
      } catch (Exception ex) {

      } finally {
        try {
          br.close();
        } catch (IOException ex) {
          Logger.getLogger(PersonExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

  }

  private boolean isHashTableName(String s) {
    return HashTableName.containsKey(s.toLowerCase());
  }

  ///Extension  
  public static class OnlyExt implements FilenameFilter {
    String ext;

    public OnlyExt(String ext) {
      this.ext = "." + ext;
    }

    public boolean accept(File dir, String name) {
      return name.endsWith(ext);
    }
  }
// Find word match :
   String word = //some function here to extract word;

    namebymatching = "NOT" + filename; //filename should be here
    if (isHashTableName(spannedText))
      namebymatching = "ISPARTOF" +filename;//filename should be here
哈希表哈希表名称;
公共字符串名称匹配;
//比较文件夹中每个词典中单词的跨距文本
public OneExtractor()引发IOException{
超级();
//位置
HashTableName=新的Hashtable();
FilenameFilter ff=newonlyext(“txt”);
文件夹=新文件(“/文件夹路径/”);
File[]files=文件夹.listFiles(ff);
Map Map=newlinkedhashmap();
对于(int i=0;i
您可以使用另一个
哈希表
来管理您的
哈希表
集合!!如果您想稍微现代一点,可以使用
HashMap
。可以使用将文件映射到内部哈希表的外部哈希表,然后可以分析内部哈希表。对于您找到的每个文件,在外部哈希表中添加一个条目,然后对于每个条目,执行您为该文件计算的过程。

如果不需要同步,首先使用
HashMap
。我想我已经在上面的代码中应用了HashMap。“Map Map=new LinkedHashMap();”但是当我为了上面的目的扩展这段代码时,我可以看到性能太慢了。这真的花了我一天的时间没有告诉我结果。。。