Java, reading a file from current directory?

前端 未结 7 746
自闭症患者
自闭症患者 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:28

    The current directory is not (necessarily) the directory the .class file is in. It's working directory of the process. (ie: the directory you were in when you started the JVM)

    You can load files from the same directory* as the .class file with getResourceAsStream(). That'll give you an InputStream which you can convert to a Reader with InputStreamReader.


    *Note that this "directory" may actually be a jar file, depending on where the class was loaded from.

    0 讨论(0)
提交回复
热议问题