Download presentation
Presentation is loading. Please wait.
Published byShonda Pierce Modified over 9 years ago
1
CIS 234: File Input & Output introduction
2
Data Storage Is Fundamental computers need to store data to work with it memory (RAM) is fast but transient lost when computer turned off storage devices are slow but persistent diskette, hard drive, tape, CD-ROM, DVD
3
Files on External Devices data accounting data in "flat" files data in database files reports in word processor files images in graphics files stored programs in files
4
Using Data Files with Java input/output classes in Java import java.io.* file class constructor File myData = new File("my.txt"); // on same directory as class file can also include file path "A:\\Project9\\my.txt" //note double slashes (escape char)
5
Files Have Properties name – file identifier + extension mydatatxt parent – drive + directory(s) A:\My Documents\Project8\ path – parent + name A:\My Documents\Project8\mydata.txt
6
File Properties - 2 readable – by the program writeable – can save data into it length – file size in bytes last modified – date last saved
7
File Class Methods canRead() – readable (true/false) if (myData.canRead()) { some code} canWrite() – can save to it (true/false) length() – size lastModified() – date
8
File Class Methods - 2 getName() String fn = 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 if not there
9
File Organization "Hierarchy" a hierarchy is like an outline from top down: file has records (1 or more), which have fields, which are made up of characters, which are made up of bits
10
OS Opens File sets up an area in memory to receive data sends message to storage device finds location of file data on media file allocation table in DOS may lock file so other programs can't use it
11
OS Closes File may write unsaved data to medium frees up memory unlocks file
12
Input and Output Devices Java, C, etc. programs use "standard" input and output devices defaults input comes from keyboard output 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 COBOL works with records as a stream of data better for multiplatform systems Java and C work with streams
14
Creating an Output File two ways to associate file with an output stream pass filename argument to FileOutputStream constructor pass filename argument to File constructor, then pass file object to FileOutputStream constructor
15
Read/write text files Read – Input FileReader infile = new FileReader(“data.txt”); BufferedReader in = new BufferedReader(infile); in.readLine(); // test end of data (inStr != null) Write – output FileWriter outStream = new FileWriter(“data.txt”); PrintWriter out = new PrintWriter(outStream); Out.println( “myData” + “|” + name);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.