Java - Getting file from same package

前端 未结 4 1566
甜味超标
甜味超标 2021-01-11 13:16

If I want to read from \"Words.txt\" which is in the same package as the class, how would I do this? Doing simply Scanner = new Scanner(new File(\"Words.txt\"));

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 13:35

    Scanner scanner = new Scanner(getClass().getResourceAsInputStream("Words.txt"));
    
    String s = new String();
    
    while(scanner.hasNextLine()){
    
    
            s = s + scanner.nextLine();
    
    
     }
    

提交回复
热议问题