0

I am currently new with javaC. I have installed JDK and set the path to make it work. I have already done several test programs and they worked.

Let's say I have a java file called Read.java and a text file called Numbers.txt

I have already set my directory to where the files are and I enter to command

javac Read.java

then

java Read < input.txt

Problem is how I can set Read.java program to receive the input.txt file? I know you can read the file from the program itself without redirection. But I want to learn how you can read a file using redirection.

user3345335
  • 33
  • 1
  • 1
  • 6

3 Answers3

0

Java's main method looks something like:

public static void main(String[] args)
{
    // method body
}

args is an array of parameters that the user can pass to the program - the first parameter would be args[0], the second args[1] and so on.

To receive the input text file, you can have the user type java Read input.txt. input.txt will be the first parameter, and so you can access it by using args[0] in your main method.

A simple example of command line arguments:

public static void main(String[] args)
{
    String input = args[0];
    System.out.println("You entered: " + input);
}

You can run this by typing java ProgramName hello, and the output will be You entered hello.

helencrump
  • 1,351
  • 1
  • 18
  • 27
  • Edited - does that help you to understand `args`? – helencrump Sep 02 '15 at 22:10
  • ahhh, okay that's something new I didn't know about. But what I was trying to find out was typing the command on top (java Read < input.txt) which is similar to Scanner sc = new Scanner(new File("input.txt")) and then printing each line to the screen. Didn't know how to set the body to read the input.txt file from command – user3345335 Sep 02 '15 at 23:34
  • If you want to use a scanner, you should be able to do something like `Scanner sc = new Scanner(new File(args[0]))`. `args[0]` is just a string that represents the file name, which the user will enter as a parameter. – helencrump Sep 03 '15 at 09:14
0

You need to read from standard input:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class IORedirection {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String line;
        while((line = in.readLine()) != null){
            System.out.println(line);
        }
    }
}  

> echo "hello stdin" | java IORedirection
> hello stdin

0

how I can set Read.java program to receive the input.txt file? I know you can read the file from the program itself without redirection. But I want to learn how you can read a file using redirection.

There are several ways to get input to your program. This isn't about "Java", but rather what are the ways for the caller to write data to "standard input" (or "stdin"). Within any Java program, you can read stdin with System.in.

So, use System.in within your program, and then use a pipe (|) or a redirect (<). Below are two working examples from an answer I posted on a related question:

% cat input.txt | java SystemInExample.java
% java SystemInExample.java < input.txt 
Kaan
  • 5,434
  • 3
  • 19
  • 41