-1

I need that my program would support the operation :

java -jar filename.jar < input3.txt

I don't realy know how to deal with such command. How can I read the input file in my main program? I need to read the txt file line by line.

Tomarlin
  • 63
  • 7

2 Answers2

2

You have the answer for your question in this post.

The main method of your program has to be ready to accept the file path in its arguments (and do what it has to do with the file).

If the file path is the first argument, then you can access it through the first position of the arguments array.

public class MainClass {

    public static void main(String[] args) {

          String filePath = args[0];
    }
}

To execute, you'll just have to do:

    java -jar filename.jar input3.txt
Community
  • 1
  • 1
José Belo
  • 139
  • 1
  • 8
  • how I accept this file path in its arguments? btw i don't know the inputfile name nor location – Tomarlin Apr 21 '14 at 15:39
  • @user3556996 In your `main(String[] args)` the first argument will be the filename if `java -jar filename.jar input3.txt` is run. It is already good to accept input you just need to seperate by a space and then it will be that position in the array if that makes sense. – 3kings Apr 21 '14 at 15:43
  • The post I linked also has this information. But I edited this post anyway to provide more details. – José Belo Apr 21 '14 at 15:45
  • it works when I use java -jar filename.jar input3.txt but my teacher want me that is support the command with the "<": java -jar filename.jar < input3.txt – Tomarlin Apr 21 '14 at 15:53
0

I found a solution, i used the command

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

for the input

Tomarlin
  • 63
  • 7