
-----------------------------------
rizzix
Mon Jun 02, 2003 6:14 pm

[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)

import java.io.*;


then the rest is as follows:

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.

-----------------------------------
DanShadow
Wed Oct 20, 2004 7:44 pm


-----------------------------------
Ok, so that works with reading from a file. But could you explain how to write to a file? tx

-----------------------------------
zylum
Thu Oct 21, 2004 6:10 pm


-----------------------------------
that only explains reading keyboard input  :?

-----------------------------------
wtd
Thu Oct 21, 2004 6:16 pm


-----------------------------------
I think...

BufferedReader inputFile = new BufferedReader(new FileReader("foo.txt"));

Manipulate it the same way you would console I/O.

-----------------------------------
Hikaru79
Mon Aug 01, 2005 12:27 pm


-----------------------------------
Ok, so that works with reading from a file. But could you explain how to write to a file? tx
As for File Output:
import java.io.*;

public class FileOutput{
	
	public static void main ( String 
