Java, reading a file from current directory?

前端 未结 7 791
自闭症患者
自闭症患者 2020-12-02 06:37

I want a java program that reads a user specified filename from the current directory (the same directory where the .class file is run).

In other words, if the user

7条回答
  •  被撕碎了的回忆
    2020-12-02 07:24

    None of the above answer works for me. Here is what works for me.

    Let's say your class name is Foo.java, to access to the myFile.txt in the same folder as Foo.java, use this code:

    URL path = Foo.class.getResource("myFile.txt");
    File f = new File(path.getFile());
    reader = new BufferedReader(new FileReader(f));
    

提交回复
热议问题