44

I am not sure how you are supposed to read in from system input from a Java file.

I want to be able to call java myProg < file

Where file is what I want to be read in as a string and given to myProg in the main method.

Any suggestions?

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
Alex
  • 5,364
  • 9
  • 54
  • 69
  • What's the problem exactly? 1) you don't know how to start a java program with an argument 2) you don't know how to open a file within a java program which has the filename as an argument of the main method. or 3) both – Fortega Mar 30 '11 at 15:02
  • 2
    @Fortega neither of those. He wants to pipe a file in in lieu of system input. – corsiKa Mar 30 '11 at 15:04
  • 1
    @Peter sometimes reading 68 million pages is a little TOO much! – corsiKa Mar 30 '11 at 15:12
  • Sorry, didn't see you wanted to read from a file http://www.google.co.uk/search?q=java+read+in+from+file+example 754 million hits. – Peter Lawrey Mar 30 '11 at 15:13
  • @glowcoder, You may be able to stop reading once you get the genral idea. ;) – Peter Lawrey Mar 30 '11 at 15:15
  • 1
    Jarrod, this question was written four years before the question you've marked this a duplicate of... – Alex Mar 29 '16 at 18:44

8 Answers8

68

You can use System.in to read from the standard input. It works just like entering it from a keyboard. The OS handles going from file to standard input.

import java.util.Scanner;
class MyProg {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Printing the file passed in:");
        while(sc.hasNextLine()) System.out.println(sc.nextLine());
    }
}
Macintosh Fan
  • 355
  • 1
  • 4
  • 18
corsiKa
  • 81,495
  • 25
  • 153
  • 204
19

Well, you may read System.in itself as it is a valid InputStream. Or also you can wrap it in a BufferedReader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
xappymah
  • 1,624
  • 10
  • 14
  • 3
    Wrapping in a BufferedReader seems a bad idea because on Linux (perhaps not in every configuration) it leads to your program not getting the data after you type something in the terminal/PuTTy and press "Enter". In order for the program to get what you've typed, you need to send some special character to flush the stream. – Serge Rogatch Jan 31 '15 at 19:23
7

In Java, console input is accomplished by reading from System.in. To obtain a character based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream. Its most commonly used constructor is shown here:

BufferedReader(Reader inputReader)

Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters.

To obtain an InputStreamReader object that is linked to System.in, use the following constructor:

InputStreamReader(InputStream inputStream)

Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:

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

After this statement executes, br is a character-based stream that is linked to the console through System.in.

This is taken from the book Java- The Complete Reference by Herbert Schildt

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
5

Use System.in, it is an InputStream which just serves this purpose

Riccardo Cossu
  • 2,699
  • 1
  • 28
  • 47
4

You would read from System.in just like you would for keyboard input using, for example, InputStreamReader or Scanner.

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48
1

You can call java myProg arg1 arg2 ... :

public static void main (String args[]) {
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
}
Adil
  • 4,503
  • 10
  • 46
  • 63
Collin Price
  • 5,620
  • 3
  • 33
  • 35
-3

You probably looking for something like this.

FileInputStream in = new FileInputStream("inputFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
  • 1
    This is quite old post. Make sure that you are carefully commenting on something that is seen by soooo many people, thus, most likely many experts already checked it. – smttsp Jun 22 '14 at 19:12
-6
class myFileReaderThatStarts with arguments
{

 class MissingArgumentException extends Exception{      
      MissingArgumentException(String s)
  {
     super(s);
  }

   }    
public static void main(String[] args) throws MissingArgumentException
{
//You can test args array for value 
if(args.length>0)
{
    // do something with args[0]
}
else
{
// default in a path 
// or 
   throw new MissingArgumentException("You need to start this program with a path");
}
}
Robert Quinn
  • 579
  • 5
  • 10