Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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.

3 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.

4 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.

5 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.

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

7 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.

8 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.

9 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.

10 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

11 Sequential Files To read a file it must be opened using the FileInputStream.

12 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

13 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.

14 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.

15 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.

16 Reading and Writing Files Examples FileCopySimple.java – Use only InputFileStream and OutputFileStream. FileCopy.java – Use buffered input and output.

17 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…

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

19 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.

20 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.

21 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


Download ppt "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."

Similar presentations


Ads by Google