Sunteți pe pagina 1din 18

CT038-3-2 Object Oriented Development with Java Managing Input Output Files

Topic & Structure of the lesson


Concept of Streams Stream Classes Byte Stream Classes Character Stream Classes Creation of files Concatenating and Buffering Files

CT038-3-2 OODJ

Managing Input Output Files

Learning outcomes
At the end of this lecture you should be able to: read and write text files become familiar with the concepts of text and binary formats

CT038-3-2 OODJ

Managing Input Output Files

Key terms you must be able to use


If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams: Streams Byte Stream Character Stream

CT038-3-2 OODJ

Managing Input Output Files

Concept of Streams
Java uses the concept of streams to represent the ordered sequence of data. A stream is a path along which data flows. It has the source and a destination. Streams are classified as input stream and output stream

CT038-3-2 OODJ

Managing Input Output Files

Input stream extracts data from the source file. Output stream takes data from the program and sends it to the destination file.

CT038-3-2 OODJ

Managing Input Output Files

Stream Classes
The java.io package contains a large number of stream classes. These classes are categorized into: 1. Byte stream provide support for handling I/O operations on bytes 2. Character stream provide support for managing I/O operations on characters

CT038-3-2 OODJ

Managing Input Output Files

Byte Stream Classes


Provide functional features for creating and manipulating streams and files for reading and writing bytes. Two types of byte stream classes are input stream classes and output stream classes.

CT038-3-2 OODJ

Managing Input Output Files

Character Stream Classes


Two types of character stream classes are reader stream classes and writer stream classes.

CT038-3-2 OODJ

Managing Input Output Files

Creation of files
Suitable name for the file Data type to be stored Purpose ( reading or writing) Method of creating the file

CT038-3-2 OODJ

Managing Input Output Files

For using a file , it must be opened first. This is done by creating a file stream and then linking it to the filename. Use Reader/InputStream or Writer/OutputStream

CT038-3-2 OODJ

Managing Input Output Files

Example:
FileInputStream fis; // Declare a file stream object try { // Assign the filename to the file stream object fis = new FileInputStream(test.data) . } catch (IOException e)

CT038-3-2 OODJ

Managing Input Output Files

Reading / Writing Characters


Example: Using two file stream classes to copy the contents of a file named input.dat into a file named output.dat.
import java.io.*; class Characters { public static void main (String args[]) { //Declare and create input and output files File inFile = new File(input.dat); File outFile = new File(output.dat); FileReader ins = null; FileReader outs = null;

CT038-3-2 OODJ

Managing Input Output Files

try { ins = new FileReader(inFile); outs = new FileReader(outFile); int ch; while ((ch=ins.read()) !=-1) { outs.write(ch); } } catch(IOException e) { System.out.println(e); System.exit(-1); } finally { try { ins.close(); outs.close(); } catch(IOException e){} } } }
CT038-3-2 OODJ Managing Input Output Files

Writing bytes to a file


Example: The program writes the names of some cities stored in array to a new file named city.txt.
import java.io.*; class WriteBytes { public static void main (String args[]) {

byte cities[] = {};


FileOuputStream outfile = null; try { outfile = new FileOutputStream(city.txt); outfile.write(cities); outfile.close();

}
catch (IOException ioe){} } }

CT038-3-2 OODJ

Managing Input Output Files

Reading bytes to a file


import java.io.*; class ReadBytes { public static void main (String args[]) { FileInputStream infile = null; int b: try { infile = new FileInputStream(args[0]); while ((b=infile.read()) !=-1) { System.out.print((char) b); } infile.close() } catch (IOException ioe){} } }

CT038-3-2 OODJ

Managing Input Output Files

Concatenating and Buffering Files


It is possible to combine two or more input streams into a single input stream. This is done using the SequenceInputStream class. Buffer is created to store temporarily data that is read or written to a stream. Create using BufferedInputStream and BufferedOutputStream classes.
CT038-3-2 OODJ Managing Input Output Files

Example:
import java.io.*; class SequenceBuffer { public static void main (String args[]) throws IOException { FileInputStream file1 = null; FileInputStream file2 = null; SequenceInputStream file3 = null; file1 = new FileInputStream(text1.dat); file2 = new FileInputStream(text2.dat); file3 = new SequenceInputStream(file1,file2); BufferedInputStream inBuffer = new BufferedInputStream(file3); BufferedOutputStream outBuffer = new BufferedOutputStream(System.out); int ch; while ((ch = inBuffer.read()) !=-1) { outBuffer.write((char)ch); } inBuffer.close(); outBuffer.close(); file1.close(); file2.close(); } }
CT038-3-2 OODJ Managing Input Output Files

S-ar putea să vă placă și