javaLecture2.pptx

42
Simple Java I/O

Transcript of javaLecture2.pptx

Page 1: javaLecture2.pptx

Simple Java I/O

Page 2: javaLecture2.pptx

File Basics• Recall that a file is block structured. What

does this mean?• What happens when an application opens

or closes a file? • Every OS has its own EOF character and, for

text files, its own EOL character(s).

Page 3: javaLecture2.pptx

Streams

• Java file I/O involves streams. You write and read data to streams.

• The purpose of the stream abstraction is to keep program code independent from physical devices.

• Three stream objects are automatically created for every application: System.in, System.out, and System.err.

Page 4: javaLecture2.pptx

Types of Streams

• There are 2 kinds of streams– byte streams– character streams

Page 5: javaLecture2.pptx

Character Streams

• Character streams create text files.• These are files designed to be read with a text

editor.• Java automatically converts its internal

unicode characters to the local machine representation (ASCII in our case).

Page 6: javaLecture2.pptx

Byte Streams• Byte streams create binary files. • A binary file essentially contains the

memory image of the data. That is, it stores bits as they are in memory.

• Binary files are faster to read and write because no translation need take place.

• Binary files, however, cannot be read with a text editor.

Page 7: javaLecture2.pptx

Classes

• Java has 6 classes to support stream I/O• File: An object of this class is either a file or a

directory.• OutputStream: base class for byte output

streams• InputStream: base class for byte input

streams

Page 8: javaLecture2.pptx

• Writer: base class for character output streams.

• Reader: base class for character input streams.

• RandomAccessFile: provides support for random access to a file.

• Note that the classes InputStream, OutputStream, Reader, and Writer are abstract classes.

Page 9: javaLecture2.pptx

File classFile myDir = new File(“C:\\CS311”);

File myFile = new File(“C:\\CS311\\junk.java”);

File myFile = new File(“C:\\CS311”, “junk.java”);

File myFile = new File(myDir, “junk.java”).

Page 10: javaLecture2.pptx

File methods

• exists()• isDirectory()• isFile()• canRead()• canWrite()• isHidden()• getName()

Page 11: javaLecture2.pptx

• getPath()• getAbsolutePath()• getParent()• list()• length()• renameTo( newPath )• delete()• mkdir()• createNewFile()

Page 12: javaLecture2.pptx

Reading and Writing a Text File

Page 13: javaLecture2.pptx

Reading and Writing Text Files

• To write to a text file, use a PrintWriter• To read from a text file use– InputStreamReader: to read one char at a time– BufferedReader: read one line at a time– StreamTokenizer: read one word at a time

Page 14: javaLecture2.pptx

FileWriter Class• The FileWriter class is a convenience

class for writing character files.• One version of the constructor take a string for a file name, another version takes an object of the File class.

• Both versions of the constructor above have forms that take an additional boolean. If true, the data is appended to the file; if false, the file is overwritten.

Page 15: javaLecture2.pptx

PrintWriter

• PrintWriter is a useful class for making text files because it has methods print() and println()

• One version of the constructor takes an FileWriter object and a boolean.

• If the boolean it true, then the stream is flushed whenever a println() is called.

Page 16: javaLecture2.pptx

Example Disk = new PrintWriter(

new ( FileWriter ( “my_file.txt” ), true);

Disk.println( “Hello World” ); Disk.close();

See FileWrite.java

Page 17: javaLecture2.pptx

Reading One Char at a Time

• See StreamReader.java• The read() method returns an integer• This integer should be cast to a char• A value of -1 indicates the end of the stream

has been reached.

Page 18: javaLecture2.pptx

Reading One Line at a Time

• See LineReader.java• Use a BufferedReader• The readLine() method returns a String.• If the String is null, then the end of the

stream has been reached.

Page 19: javaLecture2.pptx

Reading One Word at a Time• See WordReader.java• Use a StreamTokenizer – ttype: an int that contains the type of the

current token. Values are TT_EOF, TT_EOL, TT_WORD, TT_NUMBER, or a character.

– sval: String containing the current token if it is a word

– nval: double containing the current token if it is– a number

Page 20: javaLecture2.pptx

Reading and Writing Binary Files

Page 21: javaLecture2.pptx

Reading and Writing Binary Files

• To read and write binary files, use DataInputStream and DataOutputStream

Page 22: javaLecture2.pptx

DataOutputStream Methods• writeByte( int value )• writeBoolean( boolean value )• writeChar( int value )• writeShort( int value )• writeInt( int value )• writeLong( long value )• writeFloat( float value )• writeDouble( double value )

Page 23: javaLecture2.pptx

String Output

• Writing Strings– writeBytes( String s ) //for Strings– write( byte[] b, int offset,

int length ) //partial strings

–A String may be converted to a byte array by using the String method getBytes()

– If you use writeChars( String s ) you will get Unicode characters (2 bytes).

Page 24: javaLecture2.pptx

Other Methods

• flush()• size() //number of bytes written• close()

• The constructor for this class takes an object of the outputStream class.

Page 25: javaLecture2.pptx

Filter Input Streams

• Derived from the abstract class InputStream• Some methods– read() reads single byte of data and returns it as

type int– read( byte [] b ) reads enough data to fill the

array or until the end of the steam is reached. Returns the number of bytes read.

Page 26: javaLecture2.pptx

– read( byte [] b, int offset, int length )

reads length bytes into array b beginning at position b[ offset ] returns the number of bytes read.

– skip ( long n ): reads and discards n bytes for the stream

– markSupported()– mark( int limit )– reset()– close()

Page 27: javaLecture2.pptx

DataInputStream• Extends FilterInputStream.• The methods in this class are mostly a

mirror of the methods in the DataOutputStream class.

• This class does throw an EOFException when the end of the stream is found.

• See the example BinaryStreamTest.java and BinaryReadWrite.java.

Page 28: javaLecture2.pptx

Random Access Files

• A random access file allows you to read and write a file at any point.

• Methods that move the file pointer– seek( long position )– getFilePointer(): returns long– length(): returns long

Page 29: javaLecture2.pptx

• Constructor for a random access file– the constructor takes two arguments.– The first identifies the file– The second is “rw” or “r”RandomAccessFile F = new RandomAccessFile( “myFile”, “rw” )

• To use a random access file successfully, the data must be broken into fixed size units.

• See RandomFileTest.java

Page 30: javaLecture2.pptx

Object Streams

• To read and write objects, do the following– make the class serializable by adding implements Serializable to the class definition

– Use the ObjectInputStream and ObjectOutputStream classes along with the writeObject() and readObject() methods.

Page 31: javaLecture2.pptx

Object Streams II

• Class instance variables can be marked as transient to avoid having their values written to a file. For example the next field in a linked list object or a current time field would normally be transient.

• See ObjectFile.java for an example.

Page 32: javaLecture2.pptx

Random Access with Objects• A random access file must have each “slot”

in the file the same length. This is fine if I only want to read and write a primitive type, but what if I want to read or write an object?

• In this case, I must do my own serialization. I must also make all strings fields a fixed size.

• See RandomObject.java

Page 33: javaLecture2.pptx

Streams

• All modern I/O is stream-based• A stream is a connection to a source of data or to a

destination for data (sometimes both)• An input stream may be associated with the keyboard• An input stream or an output stream may be

associated with a file • Different streams have different characteristics:

– A file has a definite length, and therefore an end– Keyboard input has no specific end

Page 34: javaLecture2.pptx

How to do I/O

import java.io.*;

• Open the stream• Use the stream (read, write, or both)• Close the stream

Page 35: javaLecture2.pptx

Why Java I/O is hard

• Java I/O is very powerful, with an overwhelming number of options

• Any given kind of I/O is not particularly difficult

• The trick is to find your way through the maze of possibilities

openuseclose

Page 36: javaLecture2.pptx

Opening a stream

• There is data external to your program that you want to get, or you want to put data somewhere outside your program

• When you open a stream, you are making a connection to that external place

• Once the connection is made, you forget about the external place and just use the stream

openuseclose

Page 37: javaLecture2.pptx

Example of opening a stream• A FileReader is a used to connect to a file

that will be used for input: FileReader fileReader =

new FileReader(fileName);• The fileName specifies where the (external)

file is to be found• You never use fileName again; instead, you

use fileReader

openuseclose

Page 38: javaLecture2.pptx

Using a stream

• Some streams can be used only for input, others only for output, still others for both

• Using a stream means doing input from it or output to it

• But it’s not usually that simple--you need to manipulate the data in some way as it comes in or goes out

openuseclose

Page 39: javaLecture2.pptx

Example of using a stream

int ch;ch = fileReader.read( );

• The fileReader.read() method reads one character and returns it as an integer, or -1 if there are no more characters to read

• The meaning of the integer depends on the file encoding (ASCII, Unicode, other)

openuseclose

Page 40: javaLecture2.pptx

Manipulating the input data

• Reading characters as integers isn’t usually what you want to do

• A BufferedReader will convert integers to characters; it can also read whole lines

• The constructor for BufferedReader takes a FileReader parameter: BufferedReader bufferedReader =

new BufferedReader(fileReader);

openuseclose

Page 41: javaLecture2.pptx

Reading lines

String s;s = bufferedReader.readLine( );

• A BufferedReader will return null if there is nothing more to read

openuseclose

Page 42: javaLecture2.pptx

Closing

• A stream is an expensive resource• There is a limit on the number of streams that

you can have open at one time• You should not have more than one stream

open on the same file• You must close a stream before you can open

it again• Always close your streams!

openuseclose