CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007.
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007.
Data Storage Is Fundamental computers need to store data to
work with it memory (RAM) is fast but transient
data is lost when computer is turned off storage devices are slow but
persistent ("permanent storage") hard drive, diskette, CD-ROM, DVD ,
tape speeds (scroll down to L1 cache; 104)
Files on External Devices data
accounting data in "flat" files data in database files documents in word processor files images in graphics files
stored programs in files some early computers did not use
files for their programs (Altair)
Using Data Files with Java input/output classes in Java
import java.io.* file class constructor
File myData = new File("my.txt");// in same directory as class file
can also include file path"A:\\Project9\\my.txt"
//note double slashes (1st=escape char)
Files Have Properties name = file identifier + extension
mydata txt parent = drive + directory(s)
A:\My Documents\Project5\ full path = parent + name
A:\My Documents\Project5\mydata.txt
File Properties - 2 readable – by a program writeable – can save data into it length – file size in bytes last modified – date last saved etc.
File Organization "Hierarchy" a hierarchy is like an outline from top down (in a "flat" file):
file has records (1 or more) which have fields (1 or more) which are made up of character(s) (each is 2 bytes) made up of bits
what's above the top?
Java's File Class Methods to get data about a file
canRead() – readable (true/false)if (myData.canRead()) { some
code} canWrite() – can save to it (true/false) length() – size in bytes lastModified() – date
File Class Methods - 2 can break a full path into parts
getName() //from full path String fileName = myData.getName();
getPath() //if identified when created getParent() //if identified when created
exists() – check to see if it's there don't try to read it if it's not there
Operating System Opens File sets up an area in memory to
receive data sends message to storage device finds location of file data on media
in DOS, uses file allocation table (FAT) may lock file so other programs
can't use it at same time
OS Closes File may write unsaved data to output
medium frees up memory unlocks file
if it was locked while being used
Input and Output Devices Java, C, etc. programs use "standard"
input and output (SIO) devices defaults
input (System.in) comes from keyboard output (System.out) goes to screen
can override defaults e.g., send output to a file or printer
"Streams" two ways to deal with data
1 record at a time ("flat" file organization)
COBOL works with records as a stream of data in individual bytes
better for multiplatform systems Java and C work with streams can be broken up with \n (newline
characters) into logical partitions
Input & Output Streams
InputStream myIStream;OutputStream myOStream;myIStream = System.in;myOStream = System.out;char c = myIStream.read();myOStream.write("abc");
Buffer area in memory to hold input or
output data temporarily after being read but before being
used in a program before being written to disk
buffers can improve performance program can read data from one part
of a buffer while more data is coming in from disk to another (part of) buffer
Creating an Output File two ways to associate a file with an
output stream pass filename argument to
FileOutputStream constructor pass filename argument to File
constructor, then pass file object to FileOutputStream constructor
Creating an Output File - 2FileOutputStream = new
FileOutputStream("mydata.txt"); //1st approach
File myFile = new File("mydata.txt");FileOutputStream = new FileOutputStream(myFile); // 2nd
approach
FileWriter class Java "convenience* class" for
writing character files* convenience means easier to use
constructors assume that default character encoding and buffer size are OK
Using FileWriter put code inside a try block create FileWriter object with filename can use write method to write outputs
or could create a PrintWriter object using a println() method with it (p.527-528)
be sure to use close() method after finish writing output file will lose all its data if it isn't closed
FileWriter Example (code)try{
FileWriter outs = new FileWriter("demoIo.txt" );
outs.write("Hello", 0, 5);//String, start, lengthouts.write("\n" + 1); // \n=new line outs.close(); // don't forget to use this line!!!} // view demoIo.txt with editor
[ catch (IOException e) {[some code]} ]
Review Compare and contrast memory (RAM,
cache) and storage devices (disk, CDs) Identify some types of data, etc. that
can be stored in files What is a “flat file?” What do you need at the top of a .java
file to use file data? import.java.___? Identify some file properties.
Review - 2 Identify some types of file handling
methods. Identify some parts of a file
identifier. Name some low level items in the
“file hierarchy”? What has to or might have to
happen when a file is opened? Is closed?