Document Details

YouthfulTsavorite7659

Uploaded by YouthfulTsavorite7659

Vietnam-France University

Tags

java programming java streams input/output

Summary

This document provides an overview of Java streams and files, including data streams, types of streams (byte streams and character streams), hierarchies for input/output streams, methods for manipulating text and binary files(PDF).

Full Transcript

Object-Oriented Programming Streams and Files Outline Concepts of Data Streams Streams and Files Manipulate Text Files Manipulate Binary Files RandomAccessFile Class 2 Data Streams Data are stored as a s...

Object-Oriented Programming Streams and Files Outline Concepts of Data Streams Streams and Files Manipulate Text Files Manipulate Binary Files RandomAccessFile Class 2 Data Streams Data are stored as a sequence of bytes: § But, we can consider data as having some higher- level structure such as being a sequence of characters or objects Streams: a sequence of data that is read from a source or written to a destination § Source: file, memory, keyboard,… § Destination: file, memory, screen,… Java programs send and receive data via objects of some data stream types 3 Data Streams Streams: may be connected to a file on floppy, a file on a hard disk, a network connection or may even just be in memory We abstract away what the stream is connected to, and just focus on performing I/O operations on the stream 4 Types of Streams Byte streams: manipulate data in bytes. Two abstract classes are provided § InputStream § OutputStream Character streams: manipulate data as Unicode text streams. Two abstract classes are provided § Reader § Writer 5 InputStream Hierarchy 6 Methods of InputStream Method Description int read() reads next byte of data from input stream int read(byte[] b) reads “b.length” bytes from input stream to array “b” int read(byte[] b, int offset, int length) reads “length” bytes from input stream to array “b”, starting from the “offset” address void close() closes input stream 7 OutputStream Hierarchy 8 Methods of OutputStream Method Description int write(int c) writes the single byte “c” to output stream int write(byte[] b) writes “b.length” bytes from array “b” to output stream int write(byte[] b, int offset, int length) writes “length” bytes of array “b”, starting from the “offset” address to output stream void close() closes output stream Void flush() flushes the data stream from buffer to output stream 9 Reader Hierarchy 10 Methods of Reader Method Description int read() reads next character from input stream int read(char[] b) reads “b.length” characters from input stream to array “b” int read(char[] b, int offset, int length) reads “length” characters from input stream to array “b”, starting from the “offset” address void close() Closes input stream 11 Writer Hierarchy 12 Methods of Writer Method Description int write(int c) writes the single character “c” to output stream int write(char[] b) writes “b.length” characters from array “b” to output stream int write(char[] b, int offset, int length) writes “length” characters from array “b”, starting from the “offset” to output stream void close() closes output stream void flush() flushes the data stream from buffer to output stream 13 Important Types of Streams InputStream/OutputStream § Base class streams with few features FileInputStream/FileOutputStream § Specifically for connecting to files BufferedInputStream/BufferedOutputStream § Improve I/O performance by adding buffers BufferedReader/BufferedWriter § Convert bytes to Unicode Char and String data 14 Input/output stream object To read or write data, we need a stream object The I/O stream object needs to be attached to a data source or a destination BufferedReader in = new BufferedReader(new FileReader(fname)); 15 Use of buffered streams Buffering is a technique to improve I/O performance § Read and write data in blocks § Reduce number of accesses to I/O devices The program writes data to the buffer instead of output devices § When the buffer is full, data in buffer is pushed to the device in blocks § We can force data to be pushed by calling flush() method The program reads data from buffer instead of input devices § When the buffer is empty, data is retrieved from the input device in blocks 16 Standard I/O streams In java.lang package System.out and System.err are PrintStream objects § Can be used directly System.out.println("Hello, world!"); System.err.println("Invalid day of month!"); System.in is an InputStream object § Used with InputStreamReader (character stream) and BufferedReader (stream with buffer) BufferedReader br = new BufferedReader( new InputStreamReader(System.in)) 17 The File class In java.io package Provides basic operations on files and directories § Create files, open files, query directory information Files are not streams 18 Create a File object File myFile; myFile = new File(“data.txt”); myFile = new File(“myDocs”, “data.txt”); Directories are treated the same as files: § File myDir = new File(“myDocs”); § File myFile = new File(myDir, “data.txt”); 19 File's methods File/directory name § String getName() § String getPath() § String getAbsolutePath() § String getParent() § boolean renameTo(File newName) File/directory status § boolean exists() § boolean canWrite() § boolean canRead() § boolean isFile() § boolean isDirectory() 20 File's methods status § long lastModified() § long length() § boolean delete() directory § boolean mkdir() § String[] list() 21 Manipulate text files Read from files § FileReader: read characters from text files § BufferedReader: buffered, read in lines Write to files § FileWriter: write characters to text files § PrintWriter: write in lines 22 Read from a text file public void readLines(String fname) { try { BufferedReader in = new BufferedReader(new FileReader(fname)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } catch (IOException e) { e.printStackTrace(); } } 23 Write to a text file public void writeLines(String fname) { try { PrintWriter out = new PrintWriter(new FileWriter(fname)); out.write(“This is the object-oriented programming course”); out.close(); } catch (IOException e) { e.printStackTrace(); } } 24 Manipulate binary files Read § FileInputStream: read data from files § DataInputStream: read primitive data § ObjectInputStream: read objects Write § FileOutputStream: write data to files § DataOutputStream: write primitive data § ObjectOutputStream: write objects 25 DataInputStream/DataOutputStream DataInputStream: read primitive data § readBoolean, readByte, readChar, readShort, readInt, readLong, readFloat, readDouble DataOutputStream: write primitive data § writeBoolean, writeByte, writeChar, writeShort, writeInt, writeLong, writeFloat, writeDouble 26 Write primitive data import java.io.*; public class TestDataOutputStream { public static void main(String args[]) { int a[] = {65, 75, 86, 67, 98}; try { // file name is entered as args FileOutputStream fout = new FileOutputStream(args); DataOutputStream dout = new DataOutputStream(fout); for (int i=0; i

Use Quizgecko on...
Browser
Browser