-1

I want to go to command line and type the input, so the BufferReader can have access to the file. How am i supposed to do that ?

The input will be "java TagMatching path_to_html_file.html"

// Importing only the classes we need
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TagMatching {

    public static void main(String[] args) {

        BufferedReader br = null;    
        // try to read the file
        try {

            br = new BufferedReader(new FileReader(**/*DONT KNOW WHAT TO DO*/**));
            String line;                        

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

3 Answers3

4

The input will be java TagMatching path_to_html_file.html

After the name of the app (TagMatching) you find the arguments (path_to_html_file.html) this are the String[] args of the main method, so just use them, in this case args[0]:

public static void main(String[] args) {
    BufferedReader br = null;    
    // try to read the file
    try {
        // check if there are some arguments 
        if (null != args[0] && 
            // lenght > 5 because a.html will be shortest filename
            args[0].lenght > 5 && 
            // check if arguments have the correct file extension
            args[0].endsWith(".html")) 
        {
            br = new BufferedReader(new FileReader(args[0]));
            // do more stuff
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

To get the input from the console you have to use Scanner like this;

Scanner in = new Scanner(System.in); 
System.out.println("Enter file path");
String s = in.nextLine(); //C:\\testing.txt

And to use that file path in the FIleReader use like this;

br = new BufferedReader(new FileReader(s));
Shivam
  • 649
  • 2
  • 8
  • 20
0

Exactly works with args[0].

So,br = new BufferedReader(new FileReader(args[0])); will act as the questioner intends

Joey Cho
  • 351
  • 1
  • 8