0

How do I get a FileReader to read a file using the filename as input, rather than putting a direct filepath to the file? So instead of something like

FileReader fr = new FileReader("C:file");

We have something where when we call FileReader(filename), we put the filename in as a parameter. So if I put in the command prompt:

Java FileReader input.txt

It will read the text file without me having to have put in new FileReader("C:input.txt").

4

1 回答 1

1

当您启动您的应用程序java FileReader input.txt时,在 main 方法中

public static void main(String[] args) {
    //args[0] is input.txt
    //but you still need the rest of the path e.g. C:\
    FileReader fr = new FileReader("path_to_file_location" + args[0]);
于 2013-06-08T05:46:45.420 回答