0% found this document useful (0 votes)
61 views

Java Programming Istream

1) The document discusses reading input from standard input streams in Java using BufferedReader and reading from files using FileInputStream. 2) It also covers writing output to files in Java using FileOutputStream and the write method. 3) Examples are provided to demonstrate reading a single character, entire line of text, and writing text to an output file in Java.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Java Programming Istream

1) The document discusses reading input from standard input streams in Java using BufferedReader and reading from files using FileInputStream. 2) It also covers writing output to files in Java using FileOutputStream and the write method. 3) Examples are provided to demonstrate reading a single character, entire line of text, and writing text to an output file in Java.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SOFTWARE ENGINEERING

Standard Input File Handling

READING FROM STANDARD INPUT


Instead of getting user input from the command-line, most users prefer inputting data when prompted by the program while it is already in execution. One way of ding this is with the use of streams. A stream is an abstraction of a file or a device that allows a series of items to be read or written. Streams are connected to physical devices such as keyboards, consoles and files. There are two general kinds of streams, byte streams and character streams. Byte streams are for binary data while character streams are for Unicode characters. System.in and System.out are two examples of predefined byte streams in java. The first one by default refers to the keyboard and the latter refers to console. To read characters from the key keyboard, you can use the System.in byte stream warped in a BufferedReader object. The following line shows how to do this:

READING FROM STANDARD INPUT


BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
The read method of the BufferedReader object is then used to read from the input device.

ch = (int) br.read(); //read method returns an integer

READING FROM STANDARD INPUT


Try out this example:
import java.io.*; Class FavoriteCharacter{
Public static void main(String args[])throws IOException{ System.out.println(Hi, what is your favorite character? ); Char favChar; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); favChar = (char) br.read(); System.out.println(favChar + is a good character!); } }

READING FROM STANDARD INPUT


Running this code with the character a as in input generates the following output: Hi, whats your favorite character? a a is a good character!

READING FROM STANDARD INPUT


If you prefer reading an entire line instead of reading one character at a time, you can use the readLine method. e.g.
str = br.readline();

READING FROM STANDARD INPUT


Here is a program almost similar to the preceding example but reads an entire string instead of just one character:
import java.io.*; Class FavoriteCharacter{
Public static void main(String args[]) throws IOException{ System.out.println(Hi, what is your name? ); String name; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); name = br.readLine(); System.out.println(Nice to meet you + name + !); } }

FILE HANDLING
In some cases, data inputs are stored in files. Moreover, there are also instances when we want to store the output of a certain program to a file. In a computerized enrollment system, the student data that may be used as an input to the system is most probably stored in a particular file. Then, one possible output of the system is the information about the subjects enrolled by the students. Again, the output in this case may preferably be stored in a file. As seen in this application, there is a need for reading from a file and writing to a file. You will learn about file input and output n this section.

FILE HANDLING
Reading from a file To read from a file, you can use the FileInputStream class. Here is one of the constructors of this class. FileInputStream(String filename) The constructor creates a connection to an actual file whose filename s specified as an argument. A FileNotFoundException is thrown when the file does not exist or it cannot be opened

FILE HANDLING
After creating an input stream, you can now use to read from the associated file using the read method. The read method returns an integer and it returns -1 when the end of the file is reached.

import java.io.*; public class ReadFile { public static void main(String args[])throws IOException { System.out.println("What is the name of the file to read from?"); String filename; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); filename = br.readLine(); System.out.println("Now filename + "..."); reading from "

FILE HANDLING

try{ char data; int temp; do{ temp = fis.read(); data = (char) temp; if (temp != 1) System.out.print(data); }while (temp != -1);

}catch (IOException ex) { + System.out.println("Problem from the file."); }

in

reading

FileInputStream fis = null; try{ fis = new FileInputStream(filename); }catch (FileNotFoundException ex) {

} }

FILE HANDLING
Assuming test.txt exist and it contains the text temporary file, here is a sample output of this program. What is the name of the file to read from? test.txt Now reading from temp.txt temporary file

FILE HANDLING
Writing to a file For writing to a file, you can use the FileOutputStream class. Here is one of the constructors you can use. FileOutputStream(String filename) The constructor links an output stream to an actual file to write to. A FileNotFoundException is thrown when the file cannot be opened for writing.

Once the output stream is created, you can now use the stream to write to the linked file using the write method. The method has the following signature:
void write(int b)

try{ else{ package sd1a; fos = new fos.write('q'); FileOutputStream(filename); import java.io.*; fos.write(data); }catch(FileNotFoundException ex) } { public class WriteFile { }else{ System.out.println("Fiile cannot public static void main(String args[]) fos.write(data); be opened for writing."); throws IOException } } { }while(!done); try{ System.out.println("What is the name }catch(IOException ex) of the file to be written to?"); boolean done = false; { int data; System.out.println("Problem in String filename; reading from the file."); BufferedReader br = new do{ } BufferedReader(new data = br.read(); InputStreamReader(System.in)); if((char) data == 'q') { } filename = br.readLine(); data = br.read(); } System.out.println("Enter data to if((char)data == '$') write to " + filename +"..."); { System.out.println("Type q$ to end."); done = true; } FileOutputStream fos = null;

FILE HANDLING

You might also like