How to read a single word (or line) from a text file Java?

后端 未结 4 2128
不思量自难忘°
不思量自难忘° 2021-01-13 13:25

Like the title says, im trying to write a program that can read individual words from a text file and store them to String variables. I know how to use a

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-13 14:08

    These are all really complex answers. And I am sure they are all useful. But I prefer the elegantly simple Scanner :

    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(new File("fileName.txt"));
        while(sc.hasNext()){
            String s = sc.next();
            //.....
        }
    }
    

提交回复
热议问题