Computer Science Canada

[Tutorial] Read / Write to Terminal

Author:  rizzix [ Mon Jun 02, 2003 6:14 pm ]
Post subject:  [Tutorial] Read / Write to Terminal

To get input from Terminal.

import all classes from the java.io package. (we will be using only 3 of the imported classes though)
Java:

import java.io.*;


then the rest is as follows:
Java:

BufferedReader in;
PrintStream out;
String my_string;

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

my_string = in.readLine();
out.println(my_string);

my_string = in.readLine();
out.println(my_string);


Here we declared 2 refrences of type: BufferedReader and PrintStream.
The BufferedReader refrence is used to refer to an object of type BufferedReader, which has methods that can be used to read lines of text from anywhere. In this case System.in.

The BufferedReader object takes an InputStreamReader object as its arguemens for its Constructor. Which in turn initializes its self with an InputStream object.

The System.in object is of type InputStream.

Thus we create new instances of the necessary objects passing them the appropriate objects as arguments, finally creating the BufferedReader object to use.

There is also a public int read() method for the BufferedReader objects to read a single character at a time.

We used the out reference to refer to the System.out object.

Author:  DanShadow [ Wed Oct 20, 2004 7:44 pm ]
Post subject: 

Ok, so that works with reading from a file. But could you explain how to write to a file? tx

Author:  zylum [ Thu Oct 21, 2004 6:10 pm ]
Post subject: 

that only explains reading keyboard input Confused

Author:  wtd [ Thu Oct 21, 2004 6:16 pm ]
Post subject: 

I think...

code:
BufferedReader inputFile = new BufferedReader(new FileReader("foo.txt"));


Manipulate it the same way you would console I/O.

Author:  Hikaru79 [ Mon Aug 01, 2005 12:27 pm ]
Post subject: 

DanShadow wrote:
Ok, so that works with reading from a file. But could you explain how to write to a file? tx

As for File Output:
Java:
import java.io.*;

public class FileOutput{
       
        public static void main ( String [] args ){
                try{
                        BufferedWriter out = new BufferedWriter (new FileWriter("MyFile.txt"));
                        out.write("Hello! I am creating a new file."
                        + " If a file named \"My File.txt\" existed already, you would lose it!");
                        out.close();
                        BufferedWriter outAppend = new BufferedWriter (new FileWriter("MyFile.txt",true));
                        outAppend.append("This is being appended to the end of the old \"MyFile.txt\"!"
                        + "This is because I passed in a second argument, 'true', to the FileWriter constructor.");
                        outAppend.close();
                } catch (IOException f) {
                        System.err.println("You must not have write permissions to this directory!");
                }
        }
}


: