




























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































(^) Byte streams are defined by using two class hierarchies. (^) At the top are two abstract classes: InputStream and OutputStream. (^) Each of these abstract classes has several concrete subclasses that handle the differences between various devices, such as disk files, network connections, and even memory buffers. (^) The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. Two of the most important are read( ) and write( ), which, respectively, read and write bytes of data.
(^) int read( ) throws IOException (^) Each time that read( ) is called, it reads a character from the input stream and returns it as (^) an integer value
import java.io.*; class BRreadLine{ public static void main(String args[]) throws IOException{ String s; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Lines of Text: "); System.out.println("Enter 'stop' to quit: "); System.out.println("--------------------------"); do{ s = bf.readLine(); System.out.println(s); }while(!s.equals("stop")); System.out.println("--------------------------"); } }
(^) Console output is most easily accomplished with print( ) and println( ) These methods are defined by the class PrintStream (which is the type of object referenced by System.out) PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ) Thus, write( ) can be used to write to the console. (^) void write(int byteval)
(^) PrintWriter is one of the character-based classes. (^) PrintWriter(OutputStream outputStream, boolean flushOnNewline) Here, outputStream is an object of type OutputStream, and flushOnNewline controls whether Java flushes the output stream every time a println( ) method is called. If flushOnNewline is true, flushing automatically takes place. If false, flushing is not automatic.
(^) When you create an input stream, if the file does not exist, then FileNotFoundException is thrown. (^) For output streams, if the file cannot be created, then FileNotFoundException is thrown. (^) When an output file is opened, any preexisting file by the same name is destroyed. (^) When you are done with a file, you should close it by calling close( ). It is defined by both FileInputStream and FileOutputStream, as shown here: (^) void close( ) throws IOException
(^) To read from a file, you can use a version of read( ) that is defined within FileInputStream. The one that we will use is shown here: (^) int read( ) throws IOException (^) Each time that it is called, it reads a single byte from the file and returns the byte as an integer value. (^) To write to a file, you can use the write( ) method defined by FileOutputStream. Its simplest form is shown here: (^) void write(int byteval) throws IOException (^) This method writes the byte specified by byteval to the file.