You are on page 1of 9

On Input and Output Streams The concept of a STREAM A stream is a flow of data.

The data might be characters, numbers, or bytes consisting of binary digits. INPUT STREAM If the data flow into the program, the stream is called an INPUT STREAM. OUTPUT STREAM If the data flow out from the program, it is called an output OUTPUT STREAM. In Java, streams are implemented as objects of stream classes. These classes are defined in the java.io package(Library). Hence, programs that use the stream classes must have the import java.io.* statement.

I.

Getting Inputs from the keyboard ( Using the KEYBOARD for input)

If an input stream is connected to the keyboard, data flow from the keyboard into the program. import java.io.*; public class KeyboardInputDemo{ private BufferedReader keyboard; public void run() throws IOException {
keyboard=new BufferedReader(new InputStreamReader(System.in));

int dividend, divisor, quotient; try{ System.out.print("Enter the dividend: "); System.out.flush(); dividend = Integer.parseInt( keyboard.readLine()); System.out.println(); System.out.print("Enter the divisor: "); System.out.flush(); divisor = Integer.parseInt( keyboard.readLine()); System.out.println(); quotient = dividend/divisor; System.out.println("Quotient = " + quotient); } }// end run

public static void main(String[] args) { KeyboardInputDemo myProgram; try { myProgram = new KeyboardInputDemo(); myProgram.run(); } catch (ArithmeticException er1) { System.out.println("division by zero " + er1.toString()); } catch (NumberFormatException er2) { System.out.println("invalid integer " + er2.toString()); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } // end main }// end class

II. Getting input data from a text File To be able to get input data for a program from a text file, you must create an object of the BufferedReader class and connect it to the text file. import java.io.*; public class TextFileInputDemo { private BufferedReader inputStream; public void run() throws IOException { try {
inputStream = new BufferedReader( new FileReader("data.txt"));

String lineOfText = inputStream.readLine(); System.out.println("The statements in data.txt is/are: "); while ( lineOfText != null ) { lineOfText = inputStream.readLine(); System.out.println(" " + lineOfText); } inputStream.close(); } catch ( FileNotFoundException e1 ) {
System.out.println("File data.txt does not exist." + e1.toString());

} } public static void main(String[] args){ TextFileInputDemo myTry; try { myTry = new TextFileInputDemo(); myTry.run(); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } // end main } // end class

III. Writing/Sending the output program to a text file. To be able to save the output of a program in a text file, you must create an object of the PrintWriter class and connect it to the text file. import java.io.*; public class TextFileOutputDemo { private BufferedReader keyboard; private PrintWriter outputStream; // reference to PrintWriter public void run() throws IOException {
keyboard =new BufferedReader(new InputStreamReader(System.in)); // use keyboard for input

String lineOfText; try {


outputStream = new PrintWriter(new FileWriter("out.txt")); // or outputStream = new PrintWriter(new FileOutputStream("out.txt"));

} catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("Enter three lines of text "); for (int count = 1; count <= 3; count++) { System.out.print("Line " + count+ ": "); lineOfText = keyboard.readLine(); outputStream.println(count + " " + lineOfText); } outputStream.close(); // closes the text file
System.out.println("The lines of text you entered were written in out.txt.");

Note: If the file out.txt is already existing, the old contents will be lost. If it does not exist, a new, empty file is created. public static void main(String[] args) { TextFileOutputDemo myTry; try { myTry = new TextFileOutputDemo(); myTry.run(); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } } //end of the program(class)

IV. Appending the output of a program to a text file Sometimes, you want to add the program output to the end of an existing text file. This process is called appending to a file. To be able to do the process, create and object of the PrintWriter class and connect it to the text file as shown below. Notice the use of the true Boolean Expression. import java.io.*; public class TextFileAppendDemo { private BufferedReader keyboard; public void run() throws IOException {
keyboard =new BufferedReader(new InputStreamReader(System.in));

String lineOfText; char moreLine='y'; String response = ""; PrintWriter outputStream=null; try {
outputStream = new PrintWriter(new FileWriter("out.txt", true)); //or outputStream = new PrintWriter(new FileOutputStream("out.txt", true));

} catch (FileNotFoundException e1) {


System.out.println("File Opening Error" + e1.toString());

} while ( moreLine == 'y' || moreLine == 'Y') { System.out.print("Type a line of text: "); lineOfText = keyboard.readLine(); outputStream.println(lineOfText);
System.out.println("Do you have some more lines to enter <y/n>? ");

response = keyboard.readLine();

moreLine = response.charAt(0); } outputStream.close();


System.out.println("The lines of text you entered were written in out.txt.");

} public static void main(String[] args) { TextFileAppendDemo myTry; try { myTry = new TextFileAppendDemo(); myTry.run(); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } }

Consider the following approach.


import java.io.*; public class TextFileInputDemo4 { private BufferedReader inputStream; private BufferedReader kbd; private String fileName=""; // filename is taken from the command line. public void run(File fileName) throws IOException { try { kbd =new BufferedReader(new InputStreamReader(System.in)); inputStream = new BufferedReader( new FileReader(fileName)); String lineOfText = ""; System.out.println("The statements in " + fileName + " is/are: "); do { lineOfText = inputStream.readLine(); if (lineOfText != null) System.out.println(" " + lineOfText); } while ( lineOfText != null );

inputStream.close(); } catch ( FileNotFoundException e1 ) { System.out.println("File " + fileName + " does not exist." + e1.getMessage()); }

} public static void main(String[] args){ TextFileInputDemo4 myTry; File fileName = new File(args[0]); // The input file will be entered as a command line argument. // i.e. To run $ java TextFileInputDemo4 data.txt. // data.txt is args[0]. try { myTry = new TextFileInputDemo4(); myTry.run(fileName); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } // end main

// end class

The output file may be a parameter of the command line. With the following command line:

$java TestProgram data.txt result.txt args[0] = data.txt args[1] = result.txt args[0] and args[1] may be taken through statements in the main method. The may then be passed to another method such as the run method. Examples;
File fileName1 = new File(args[0]); File fileName2 = new File(args[1]);

You might also like