Top Banner
Files • Files are used to store long term data. – Typically referred to as persistent data. – A file is a group of related records. – A list of friends addresses or employee records •A record is composed of several fields. – A person’s name, mailing address, or employee information •A field is a group of characters that conveys meaning, like (first name). – Last name, street name, city, social security number
21

Files

Jan 02, 2016

Download

Documents

yetta-terrell

Files. Files are used to store long term data. Typically referred to as persistent data. A file is a group of related records. A list of friends addresses or employee records A record is composed of several fields. A person’s name, mailing address, or employee information - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Files

Files

• Files are used to store long term data.– Typically referred to as persistent data.– A file is a group of related records.

– A list of friends addresses or employee records

• A record is composed of several fields.– A person’s name, mailing address, or employee

information

• A field is a group of characters that conveys meaning, like (first name).

– Last name, street name, city, social security number

Page 2: Files

Files

• Files in JAVA:– Are sequential streams of bytes.– End with either an end-of-file marker or at

a specific byte number recorded in a system maintained administrative data structure.

– May be opened, read, written, and closed.

Page 3: Files

Files/Streams

• When a file is opened an object is created and a stream is associated with the object.– A Stream provides a communication

channel between a program and a file.

Page 4: Files

Using IO

• To read or write files your program must import java.io.

• The InputStream and OutputStream classes define methods for performing input and output.– The FileInputStream and FileOutputStream

classes define methods for file input and output.

Page 5: Files

Using IO• The DataInputStream class allows your

program to read data in bytes that form specific data types (int, boolean, float).– readInt, readChar, readDouble, etc.

• The DataOutputStream class allows your program to write data in bytes that form specific data types (int, boolean, float).– writeInt, writeChar, writeDouble, etc.

Page 6: Files

Using IO

• Buffering can be used to increase the speed of your IO.– The BufferedInputStream class reads many logical

chunks of files at once and places them into a memory buffer.

– The BufferedOutputStream class writes output to a memory buffer and when the buffer is full, the information is written to the file.

• The program can force the buffer to be written using:– testBufferedOutputStream.flush();

Page 7: Files

Using IO• The LineNumberInputStream class can be

used to know what line number in a file is being read.

• The File class allows access to information about a file or directory.– When an existing file is opened for writing all

the information stored in the file is deleted.– When done working with files, they should be closed immediately.

Page 8: Files

Exceptions

• IOExceptions can occur when:– Attempting to open a nonexistent file for

reading.– Attempting to open a file for reading

without permission.– Opening a file for writing when there is no

disk space available.

Page 9: Files

Sequential Files• Sequential files are always read and written

from the first position in the file to the eof character.– To position the file position pointer to the beginning

of the file:• input.seek(0);

• To update information in a sequential file the entire file must be read into memory, the item updated, and the entire file written back out to memory.

Page 10: Files

Sequential Files

• If a file does not exist and you want to create it, then your program can create the file with the desired file name.– output = new DataOutputStream(– new

FileOutputStream(“newfile.dat”));

– To output information:output.writeInt(someValue);

output.writeUTF(somevalue2); //UTF = String

Page 11: Files

Sequential Files

• To read a file it must be opened using the FileInputStream.

Page 12: Files

Sequential Files

• When reading information from a file, it can be read in the same format as it was written using a DataInputStream.

input = new DataInputStream(

new FileInputStream(“newFile.dat”);

accountNum = input.readInt();

name = input.readUTF(); // UTF = String

Page 13: Files

Sequential Files

• EOF stands for End of File. When EOF is found in a sequential file, the closeFile() method is called and the file is automatically closed and the program is terminated.

Page 14: Files

Sequential Files

• Sequential files must be read in order to find a particular piece of information, this can lead to reading the file many times while a program runs.

Page 15: Files

Sequential File IO Examples

• ReadNums.java – Reading a sequential file of integers using a buffered reader.

• FileEcho.java – Read a file and display it using standard output.

Page 16: Files

Reading and Writing Files Examples

• FileCopySimple.java – Use only InputFileStream and OutputFileStream.

• FileCopy.java – Use buffered input and output.

Page 17: Files

Sequential Files• In order to modify a record in a sequential

file the entire file must be read and rewritten.– If we have a file of 50 records and we need to change

the name of the person stored in the 31st record we must read records 1 to 30 and write them to a new file, then read the 31st record and write out the record to the second file with the new name and the rest of the record unchanged, and then we must read records 32 to 50 and write them out to the new file. We could also try to store them into memory but…

Page 18: Files

Sequential Files

• If a sequential file is opened for output, then the information in the file is lost.

• In order to add new information to a file, the file must be opened with the append option selected.FileOutputStream writeTo = new

FileOutputStream(“fileName”, true);

Page 19: Files

File Class• The File Class can be used to access

information about a file. It can determine– if a file can be read or written, is a file or

simply exists. – if the object provided is a directory or an

absolute directory path.– what the absolute directory path is, the file

name, the path of the file or directory, or the parent directory of a file.

Page 20: Files

File Class

– what the length of a file is in bytes. If it is a directory the method returns zero.

– when the file or directory was last modified.– what the contents of a directory are.

– See the File Class Java doc for details.

Page 21: Files

Using Streams for Input

• The InputStreamReader, BufferedReader, DataInputStream, etc. can also be used to create a stream that reads input from standard input.

• Examples: – InputNums.java– InputEcho.java