从文件中读取并添加到地图界面java

时间:2014-01-21 01:08:16

标签: java map io bufferedreader

我有一个包含这些内容的文本文件,我需要用#:

跳过所有内容
###########################################################################
#Copyright 1999 The Internet Dictionary Project/Tyler Chambers
#http://www.june29.com/IDP/
#This file is free to use and modify.  Thank you for using the IDP.
#
#Approximately 5048 entries.  9/21/97
#Approximately 5488 entries.  1/7/98
#Approximately 5910 entries.  3/8/98
#Approximately 7450 entries.  2/19/99
###########################################################################
a   un, uno, una[Article]
aardvark    cerdo hormiguero
aardvark    oso hormiguero[Noun]
aardvarks   cerdos hormigueros
aardvarks   osos hormigueros 
ab  prefijo que indica separacio/n
aback   hacia atras
aback   hacia atrás,take aback, desconcertar. En facha.
aback   por sopresa, desprevenidamente, de improviso
aback   atra/s[Adverb]
abacterial  abacteriano, sin bacterias

我需要创建一个地图> 其中key是String english,它是文本文件中的第一个单词 并列出所有符合英文关键词的西班牙语单词

我有这样的东西,但它显示NullPointerException这一行: try(BufferedReader in = new BufferedReader(new FileReader(getServletContext()。getRealPath(“/ WEB-INF / Spanish.txt”)))))

也许是因为我错了“if(!line.startsWith(”#“))”??

try (BufferedReader in = new BufferedReader(new FileReader(getServletContext().getRealPath("/WEB-INF/Spanish.txt"))))
    {
            Map<String,List<String>> map = new HashMap<String,List<String>>();

            String eng;
            List<String> spn = null;

            while (in.readLine() != null){

            String line = in.readLine();
                if (!line.startsWith("#")){
                    String[] parts = line.split(" ");
                    eng = parts[0];
                    for (int i = 1; i < parts.length; i++){
                        spn.add(parts[i]);
                    }
                    map.put(eng, spn);
                }    

            }

            in.close();
        } catch (IOException e) {
            System.out.println("File not found!");
        }

0 个答案:

没有答案