Read next word in java

前端 未结 5 1441
广开言路
广开言路 2020-11-27 18:08

I have a text file that has following content:

ac und
accipio annehmen
ad zu
adeo hinzugehen
...

I read the text file and iterate through t

5条回答
  •  渐次进展
    2020-11-27 18:54

    you're better off reading a line and then doing a split.

    File file = new File("path/to/file");
    String words[]; // I miss C
    String line;
    HashMap hm = new HashMap<>();
    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")))
    {
        while((line = br.readLine() != null)){
            words = line.split("\\s");
            if (hm.containsKey(words[0])){
                    System.out.println("Found duplicate ... handle logic");
            }
            hm.put(words[0],words[1]); //if index==0 is ur key
        }
    
    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    

提交回复
热议问题