CS5000: Foundations of Programming · 2019-10-09 · Streams Stream in Java Object that enables the...

30
CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University

Transcript of CS5000: Foundations of Programming · 2019-10-09 · Streams Stream in Java Object that enables the...

Page 1: CS5000: Foundations of Programming · 2019-10-09 · 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

CS5000:

Foundations of Programming

Mingon Kang, PhD

Computer Science, Kennesaw State University

Page 2: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 3: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 4: CS5000: Foundations of Programming · 2019-10-09 · 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

Streams

System.in: input stream

System.out: output stream

Scanner keyboard = new Scanner(System.in)

System.out.println(“Output Stream”);

Page 5: CS5000: Foundations of Programming · 2019-10-09 · 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

Text Files

Reading text files

BufferedReader stream class

Writing text files

PrintWriter stream class

Page 6: CS5000: Foundations of Programming · 2019-10-09 · 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

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;

Page 7: CS5000: Foundations of Programming · 2019-10-09 · 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

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));

Page 8: CS5000: Foundations of Programming · 2019-10-09 · 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

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.

Page 9: CS5000: Foundations of Programming · 2019-10-09 · 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

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/

Page 10: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 11: CS5000: Foundations of Programming · 2019-10-09 · 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

Exception Handling

When performing file I/O, many exceptional

situations may be thrown.

Many of these exception classes are subclasses of

the class IOException

Page 12: CS5000: Foundations of Programming · 2019-10-09 · 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

Exception Handling

BufferedReader object may throw two kinds of

exceptions

FileNotFoundException

IOException

Both exceptions should be handled

Page 13: CS5000: Foundations of Programming · 2019-10-09 · 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

Exception Handling

Opening a file should be placed inside a try

block

try{

}

catch(FileNotFoundException e){

}

catch(IOException e){

}

Page 14: CS5000: Foundations of Programming · 2019-10-09 · 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

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;

Page 15: CS5000: Foundations of Programming · 2019-10-09 · 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

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));

Page 16: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 17: CS5000: Foundations of Programming · 2019-10-09 · 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

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.

Page 18: CS5000: Foundations of Programming · 2019-10-09 · 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

Binary Files

Reading binary files

ObjectInputStream stream class

Writing binary files

ObjectOutputStream stream class

Page 19: CS5000: Foundations of Programming · 2019-10-09 · 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

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;

Page 20: CS5000: Foundations of Programming · 2019-10-09 · 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

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));

Page 21: CS5000: Foundations of Programming · 2019-10-09 · 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

Write a Binary File

ObjectOutputStream methods

writeInt

writeDouble

writeChar

writeBoolean

write

flush

close

Page 22: CS5000: Foundations of Programming · 2019-10-09 · 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

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;

Page 23: CS5000: Foundations of Programming · 2019-10-09 · 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

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));

Page 24: CS5000: Foundations of Programming · 2019-10-09 · 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

Read a Binary File

ObjectInputStream methods

readInt

readDouble

readChar

readBoolean

close

Page 25: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 26: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 27: CS5000: Foundations of Programming · 2019-10-09 · 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

RandomAccessFile

Two constrcutors

RandomAccessFile(File file, String mode)

RandomAccessFile(String name, String mode)

Page 28: CS5000: Foundations of Programming · 2019-10-09 · 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

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

Page 29: CS5000: Foundations of Programming · 2019-10-09 · 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

Binary I/O of objects

After interface subject, with serialization

Page 30: CS5000: Foundations of Programming · 2019-10-09 · 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

Reference

http://www.mkyong.com/tutorials/java-io-tutorials/