Docsity

Java File Operations - Prof. CDCR, Summaries of Database Management Systems (DBMS)

Partial preview of the text

Download Java File Operations - Prof. CDCR and more Summaries Database Management Systems (DBMS) in PDF only on Docsity!

UNIT-

Sachin D. Shelke

File Handling and Design Patterns

File Handling and Design Patterns

File Handling: Introduction, Concepts of Stream, Stream Classes, Byte Stream

Classes, Character Stream, Classes, Using Stream, and Other Useful I/O Classes,

Using the File Class, Input/output Exceptions, Creation of Files, Reading/Writing

Character, Reading/Writing Bytes, Handling Primitive Data Types, Concatenating

and Buffering Files, Random Access Files.

Design Patterns: Introduction, Types of Design Patterns, Adapter, Singleton, Iterator

Case Study: Student Management System

ByteStream

 (^) 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.

ByteStream

Character Stream

Character Stream

Reading Console Input

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

Reading Characters

To read a character from a BufferedReader, use read( ). The version of read( ) that

we will be using is

 (^) 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

Reading Characters........Example

class BRDemo {

public static void main (String[] args) throws IOException{

char c;

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

System.out.println("Enter 'q' to quit: ");

do{

c=(char)bf.read();

System.out.print(" "+c);

}while(c!='q');

Reading Strings........Example

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("--------------------------"); } }

Writing Console Output

 (^) 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 Class

 (^) 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.

PrintWriter Class

Reading and Writing Files

 (^) 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

Reading and Writing Files

 (^) 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.