CS5000: Foundations of Programming · 2019-10-09 · Streams Stream in Java Object that enables the...
Transcript of CS5000: Foundations of Programming · 2019-10-09 · Streams Stream in Java Object that enables the...
CS5000:
Foundations of Programming
Mingon Kang, PhD
Computer Science, Kennesaw State University
Files
Two types: Text file and Binary file
Text file (ASCII file)
The file data contains only ASCII values
Designed to be read by human beings
Binary file
Consist of a sequence of binary values
Designed to be read by programs
More efficient to process than text files
Streams
Stream in Java
Object that enables the flow of data between a
program and I/O device
Input Stream
Data flows into a program
Generally from the keyboard or a file
Output Stream
Data flows out of a program
Generally flow to a screen or to a file
Streams
System.in: input stream
System.out: output stream
Scanner keyboard = new Scanner(System.in)
System.out.println(“Output Stream”);
Text Files
Reading text files
BufferedReader stream class
Writing text files
PrintWriter stream class
Read a Text File
BufferedReader
Stream class that read a text file
Methods: read(…) and readLine(…)
Need to import a set of followings
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
Read a Text File
No constructor that takes a file name directly.
Use FileReader class that convert the file name
to an object for the constructor’s parameter
BufferedReader reader;
reader = new BufferedReader(new
FileReader(FileName));
Path Names
If only a file name is used as an argument, it
assumes that the file is in the same directory or
folder as the one in which the program is run
If it is not in the same directory, the full or relative
path name have to be given.
Path Names
Path names are specified depends on the operation systems
UNIX
“/user/etc/data/data.txt”
Windows
“C:\\data\\data.txt”
Reference
http://www.mkyong.com/java/how-to-construct-a-file-path-in-java/
Read a Text File
read() method
Read a single character and returns a value of integer
type
Need a type cast
readLine() method
Check FileRead1.java and FileRead2.java
char next = (char)reader.read();
Exception Handling
When performing file I/O, many exceptional
situations may be thrown.
Many of these exception classes are subclasses of
the class IOException
Exception Handling
BufferedReader object may throw two kinds of
exceptions
FileNotFoundException
IOException
Both exceptions should be handled
Exception Handling
Opening a file should be placed inside a try
block
try{
…
}
catch(FileNotFoundException e){
…
}
catch(IOException e){
…
}
Write a text file
PrintWriter
Stream class that write to a text file
Import of
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
Write a text file
Connect to the file FileName
Check JAVA document of PrintWriter
PrintWriter writer;
writer = new PrintWriter(new
FileOutputStream(FileName));
For appending:
writer = new PrintWriter(new
FileOutputStream(FileName, true));
Write a text file
May throw FileNotFoundException that
means the file could not be created for some
reasons
When a program finishes writing, it should close the
stream connection by
outputStreamName.close()
Write a text file
When writing to a file, the data is buffered rather
than physically writing to the file as soon as
possible.
The data is saved in a temporary space
When enough data is received in the buffer, the
flush() method is invoked, the buffered data is
physically written to the file all at once
The close() method invokes the method flush.
Binary Files
Reading binary files
ObjectInputStream stream class
Writing binary files
ObjectOutputStream stream class
Write a Binary File
ObjectOutputStream
Stream class that write a binary file
Import of followings
import java.io.ObjectOutputStream;
import java.io.FileOutStream;
import java.io.IOException;
Write a Binary File
Connect to a binary file
The constructor for FileOutputStream may throw a FileNotFoundException
The constructor for ObjectOutputStream may throw an IOException
ObjectOutputStream outputFile = new
ObjectOutputStream(new
FileOutputStream(FileName));
Write a Binary File
ObjectOutputStream methods
writeInt
writeDouble
writeChar
writeBoolean
write
…
flush
close
Read a Binary File
ObjectInputStream
Stream class that read a binary file
Import of followings:
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
Read a Binary File
Connect to a binary file
The constructor for FileInputStream may throw a FileNotFoundException
The constructor for ObjectInputStream may throw an IOException
ObjectInputStream inputFile = new
ObjectInputStream(new
FileInputStream(FileName));
Read a Binary File
ObjectInputStream methods
readInt
readDouble
readChar
readBoolean
…
close
Read a Binary File
If the file contains multiple types, each item type
must be read in exactly the same order it was
written to the file
RandomAccessFile
Read and write to the same file
Enable us to random access to particular parts of a
file
A random access file consists of a sequence of
numbered bytes (like an array)
seek() method: move a cursor in a file
Good in very large files
RandomAccessFile
Two constrcutors
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
RandomAccessFile
Mode
“r”: reading only
“rw”: both reading and writing to the file
“rws”: flushes the contents of the file and metadata (e.g. the modification date of the file)
“rwd”: flushes the contents of the file, but metadata might not change until the file is closed.
“rw” only flushes when flush() is invoked and doesn't change metadata until closing the file.
“rwd” is much slower for writes than “rw”, and “rws” is slower again
Binary I/O of objects
After interface subject, with serialization
Reference
http://www.mkyong.com/tutorials/java-io-tutorials/