Phirip
Phirip

Reputation: 93

Using the filename as input for FileReader

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").

Upvotes: 0

Views: 1848

Answers (1)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

When you launch your app like java FileReader input.txt, in the main method

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]);

Upvotes: 1

Related Questions