Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007.

Similar presentations


Presentation on theme: "CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007."— Presentation transcript:

1 CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007

2 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; 10 4 ) speeds

3 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)Altair

4 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 (1 st =escape char)

5 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

6 File Properties - 2 readable – by a program writeable – can save data into it length – file size in bytes last modified – date last saved etc.

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

8 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

9 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

10 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

11 OS Closes File may write unsaved data to output medium frees up memory unlocks file if it was locked while being used

12 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

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

14 Input & Output Streams InputStream myIStream; OutputStream myOStream; myIStream = System.in; myOStream = System.out; char c = myIStream.read(); myOStream.write("abc");

15 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

16 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

17 Creating an Output File - 2 FileOutputStream = new FileOutputStream("mydata.txt"); //1 st approach File myFile = new File("mydata.txt"); FileOutputStream = new FileOutputStream(myFile); // 2 nd approach

18 FileWriterFileWriter class Java "convenience* class" for writing character files * convenience means easier to use constructors assume that default character encoding and buffer size are OK

19 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

20 FileWriter Example (code)code try{ FileWriter outs = new FileWriter("demoIo.txt" ); outs.write("Hello", 0, 5);//String, start, length outs.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]} ]

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

22 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?

23 Review - 3 What does SIO mean? What are the default SIO devices? What are some differences between streams and records? Which does Java use? What is a buffer? How are buffers used in playing videos?


Download ppt "CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007."

Similar presentations


Ads by Google