Reading in from System.in - Java [duplicate]
Asked Answered
L

8

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?

Lorgnon answered 30/3, 2011 at 14:59 Comment(6)
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) bothHarp
@Harp neither of those. He wants to pipe a file in in lieu of system input.Ebon
@Peter sometimes reading 68 million pages is a little TOO much!Ebon
Sorry, didn't see you wanted to read from a file google.co.uk/search?q=java+read+in+from+file+example 754 million hits.Affra
@glowcoder, You may be able to stop reading once you get the genral idea. ;)Affra
Jarrod, this question was written four years before the question you've marked this a duplicate of...Lorgnon
E
69

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());
    }
}
Ebon answered 30/3, 2011 at 15:1 Comment(2)
don't forget to import java.util.ScannerVines
also, Scanner's documentation: docs.oracle.com/javase/7/docs/api/java/util/Scanner.htmlVines
L
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));
Lurcher answered 30/3, 2011 at 15:3 Comment(1)
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.Papain
G
8

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

Geyserite answered 9/10, 2014 at 13:47 Comment(2)
excellent explanation!Packhorse
Is clean up needed? Calling .close on the BufferedReader would call .close on the InputStreamReader, is that needed, or since System.in is not something that closes, we do not need to clean up?Ranged
H
5

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

Helyn answered 30/3, 2011 at 15:1 Comment(0)
E
4

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

Edwards answered 30/3, 2011 at 15:5 Comment(0)
S
1

You can call java myProg arg1 arg2 ... :

public static void main (String args[]) {
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
}
Simulation answered 30/3, 2011 at 15:5 Comment(0)
T
-3

You probably looking for something like this.

FileInputStream in = new FileInputStream("inputFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Torchwood answered 22/6, 2014 at 17:35 Comment(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.Phan
T
-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");
}
}
Tuneberg answered 16/7, 2013 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.