Download presentation
Presentation is loading. Please wait.
1
Section 14.1 Introduction
2
Programs and Data Files
Data information processed by word processing, spreadsheet and similar application programs are stored as data files. Java programs, written with a text editor or some Integrated Development Environment (IDE), are also data files. Programs have the capability to create and retrieve their own data files. This means that data entered in any of your programs can be saved for later use.
3
Data Organization bit byte field record file
This is the fundamental building block of all computer information, a binary digit, which stores only 1 or 0. byte One byte equals 8 bits. One character can be stored in one byte, using ASCII code. One character is stored in two bytes using Unicode. field A field is one specific unit of information of one data type, such as size, age, name, date, etc. record A record consists of a set of fields for some specific purpose, such as a student record, a medical record, an employee record, an airline passenger record, etc. file A file is a sequence of records of the same type.
4
Streams A stream is a set of bytes or characters.
Think of a stream as a pipeline that contains a neatly ordered set of data. Streams can go in two directions; for input and for output. Input streams can come from the keyboard, from an external storage source, or some GUI mouse-click. Output streams can go to the monitor, an external storage location or the printer.
5
Section 14.2 Different Types of Files
6
File Definition A file is a sequence of information stored as bytes on a disk, tape, CD or other external storage device.
7
Sequential Access & Random Access
Files can have sequential access or random access. Sequential access files allow data access only in the sequence that the data is stored in the file. Random access files allow data access in any random pattern, regardless of how the data is stored.
8
Sequential Access Examples
9
Random Access Examples
10
Text Files and Binary Files
Files are either Text Files or Binary Files. Text Files store a series of characters and can be read or edited by any text editor like Notepad or JCreator. All Java programs are text files. Binary Files have a special format that can only be read by a certain piece of software. Examples: .docx files require Word .xlsx files require Excel .pptx files require PowerPoint In this class, we will be working with Sequential Text files only!
11
Section 14.3 Reading In Text Files
12
Buffers A buffer is a temporary memory location.
Additionally, a buffer manages the transfer of data between different computer segments, such as keyboard, printer, monitor and external storage devices.
13
File Buffers External Storage RAM Chip
A file buffer is a buffer that transfers data between internal electronic memory, RAM, and external data storage on a hard drive, CD drive or jump drive. External Storage RAM Chip
14
// This first program example creates an <ExpoInfile> object.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; // #1 public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); }
15
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; // #1 public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #1 uses a Java standard input/output library, which provides access to classes for file handling.
16
Line #2 adds the keywords throws IOException.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #2 adds the keywords throws IOException. Java wants to know how you, the programmer, will deal with potential IO problems. We literally tell Java to throw away or ignore any input/output errors that may occur.
17
// This first program example creates an <ExpoInfile> object.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; // #1 public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #3 creates an f object of the ExpoInFile class. The f file structure is constructed with Data.txt. This means the internal file object f is associated with the external text file Data.txt. The ExpoInFile class contains methods for the transfer of data from external storage to internal memory. This is known as opening a file for input.
18
// This first program example creates an <ExpoInfile> object.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #4 gets down to the business of actually transferring one string of data from the Data.txt file to internal memory with the readString method. The single line of characters is stored in the memory location of the inString variable.
19
Line #5 displays the value of the inString variable.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.*; // 1 public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #5 displays the value of the inString variable. This lets you see the first line of the Data.txt file.
20
// This first program example creates an <ExpoInfile> object.
// Java1401.java // This first program example creates an <ExpoInfile> object. // The <readString> method transfers an individual string from // an external hard drive file to internal file object. import java.io.* public class Java1401 { public static void main (String args[]) throws IOException // #2 System.out.println("\nJava1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); // #3 String inString = f.readString(); // #4 System.out.println(inString); // #5 f.closeFile(); // #6 System.out.println("\n\n"); } Line #6 concludes the file handling by closing the file object. Failure to close a file object can result in data loss. Think of it as closing the chicken coop after you pick up the eggs.
21
Anything that thou openst
Computer Science Commandment Anything that thou openst -- be it a (, a [, a {, or a FILE -- thou shalt also closeth!
22
// This program demonstrates how to read in four lines from
// Java1402.java // This program demonstrates how to read in four lines from // the external "Data.txt" file. import java.io.*; public class Java1402 { public static void main (String args[]) throws IOException System.out.println("\nJava1402.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); System.out.println(f.readString()); f.closeFile(); System.out.println("\n\n"); }
23
// This program uses a <for> loop to read in file data.
// Java1403.java // This program uses a <for> loop to read in file data. // A fixed loop structure can cause problems, because the precise file size // might not be known. The last couple of <null> outputs indicate that // there was no more string data in the external text file. import java.io.*; public class Java1403 { public static void main (String args[]) throws IOException System.out.println("\nJava1403.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); for (int k = 1; k <= 10; k++) System.out.println(f.readString()); f.closeFile(); System.out.println("\n\n"); }
24
// This program attempts to read in a text file using a conditional
// Java1404.java // This program attempts to read in a text file using a conditional // loop. It seems to work, but it still reads in one line past the end of the file. import java.io.*; public class Java1404 { public static void main (String args[]) throws IOException System.out.println("\nJava1404.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = " "; while(inString != null) inString = f.readString(); System.out.println(inString); } f.closeFile(); System.out.println("\n\n");
25
// Java1405.java This program reads in the text file correctly.
// The <readString> call before the while loop and the proper placement // of the <readString> call inside the loop body cures the problem. import java.io.*; public class Java1405 { public static void main (String args[]) throws IOException System.out.println("\nJava1405.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = f.readString(); while(inString != null) System.out.println(inString); inString = f.readString(); } f.closeFile(); System.out.println("\n\n");
26
// This program demonstrates that it is possible for a program
// Java1406.java // This program demonstrates that it is possible for a program // to read in itself, since a java program is a text file. // Some of the tabs used in JCreator may look a little strange. import java.io.*; public class Java1406 { public static void main (String args[]) throws IOException System.out.println("\nJava1406.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Java1406.java "); String inString = f.readString(); while(inString != null) System.out.println(inString); inString = f.readString(); } f.closeFile(); System.out.println("\n\n");
28
Section 14.4 Writing Out Text Files
29
f.writeString("The quick brown fox jumps over the lazy dog.");
// Java1407.java // This program example creates an <ExpoOutfile> object. // The <writeString> method transfers an individual string from internal // file object to external hard drive file. import java.io.*; public class Java1407 { public static void main (String args[]) throws IOException System.out.println("Java1407.java"); String inString; ExpoOutFile f = new ExpoOutFile("Java1407.txt"); System.out.println(); f.writeString("The quick brown fox jumps over the lazy dog."); f.closeFile(); System.out.println("Java1407.txt file created\n\n"); }
30
Where is the output of this line?
// Java1407.java // This program example creates an <ExpoOutfile> object. // The <writeString> method transfers an individual string from internal // file object to external hard drive file. import java.io.*; public class Java1407 { public static void main (String args[]) throws IOException System.out.println("Java1407.java"); String inString; ExpoOutFile f = new ExpoOutFile("Java1407.txt"); System.out.println(); f.writeString("The quick brown fox jumps over the lazy dog."); f.closeFile(); System.out.println("Java1407.txt file created\n\n"); } Where is the output of this line?
31
// Java1407.java // This program example creates an <ExpoOutfile> object. // The <writeString> method transfers an individual string from internal // file object to external hard drive file. import java.io.*; public class Java1407 { public static void main (String args[]) throws IOException System.out.println("Java1407.java"); String inString; ExpoOutFile f = new ExpoOutFile("Java1407.txt"); System.out.println(); f.writeString("The quick brown fox jumps over the lazy dog."); f.closeFile(); System.out.println("Java1407.txt file created\n\n"); } Here it is!
32
// to an external hard drive file with the <writeString> method.
// Java1408.java This program transfers five strings from internal memory // to an external hard drive file with the <writeString> method. // The <writeString> method does not provide a line feed. import java.io.*; public class Java1408 { public static void main (String args[]) throws IOException System.out.println("Java1408.java"); String inString; ExpoOutFile f = new ExpoOutFile("Java1408.txt"); System.out.println(); f.writeString("Line-1 "); f.writeString("Line-2 "); f.writeString("Line-3 "); f.writeString("Line-4 "); f.writeString("Line-5 "); f.closeFile(); System.out.println("Java1408.txt file created\n\n"); }
33
// The <writelnString> method does provide a line feed.
// Java1409.java This program transfers five strings from internal memory // to an external hard drive file with the <writelnString> method. // The <writelnString> method does provide a line feed. import java.io.*; public class Java1409 { public static void main (String args[]) throws IOException System.out.println("Java1409.java"); String inString; ExpoOutFile f = new ExpoOutFile("Java1409.txt"); System.out.println(); f.writelnString("Line-1 "); f.writelnString("Line-2 "); f.writelnString("Line-3 "); f.writelnString("Line-4 "); f.writelnString("Line-5 "); f.closeFile(); System.out.println("Java1409.txt file created\n\n"); }
34
Section 14.5 Reading & Writing Simultaneously
35
Original Copy ExpoOutFile output = new ExpoOutFile("Java1410b.txt");
// Java1410.java // This program creates two file objects in the same program. // The <input> object is constructed for file reading transfer. // The <output> object is constructed for file writing transfer. // The result is that the "Java1410b.txt" file becomes a copy of the "Java1410a.txt" text file. import java.io.*; public class Java1410 { public static void main (String args[]) throws IOException System.out.println("Java1410.java\n\n"); ExpoInFile input = new ExpoInFile("Java1410a.txt"); ExpoOutFile output = new ExpoOutFile("Java1410b.txt"); inString = input.readString(); while(inString != null) output.writelnString(inString); } input.closeFile(); output.closeFile(); Original Copy
36
Section 14.6 Appending an Existing File
37
Before After // Java1411.java
// This program demonstrates the consequence of opening a file // for output that already exist. Will the next information be added // to the end of the file, or will the file be destroyed? import java.io.*; public class Java1411 { public static void main (String args[]) throws IOException System.out.println("Java1411.java\n\n"); ExpoOutFile output = new ExpoOutFile("Java1411.txt"); output.writelnString("Java 1411, line 1"); output.writelnString("Java 1411, line 2"); output.closeFile(); } Before After
38
// Java1412.java This program opens up one file for input and another file for output.
// The intend is to read in the file, store the data in an array and // then write the data back to the file, followed by new data. import java.io.*; public class Java1412 { public static void main (String args[]) throws IOException System.out.println("Java1412.java\n\n"); ExpoInFile input = new ExpoInFile("Java1412.txt"); ExpoOutFile output = new ExpoOutFile("Java1412.txt"); String temp[] = new String[1000]; int index = 0; temp[index] = input.readString(); while(temp[index] != null) index++; } input.closeFile(); for (int k = 0; k < index-1; k++) output.writelnString(temp[k]); output.writelnString("Java 1412, line 1"); output.writelnString("Java 1412, line 2"); output.closeFile(); Before After
39
// Java1412.java This program opens up one file for input and another file for output.
// The intend is to read in the file, store the data in an array and // then write the data back to the file, followed by new data. import java.io.*; public class Java1412 { public static void main (String args[]) throws IOException System.out.println("Java1412.java\n\n"); ExpoInFile input = new ExpoInFile("Java1412.txt"); ExpoOutFile output = new ExpoOutFile("Java1412.txt"); String temp[] = new String[1000]; int index = 0; temp[index] = input.readString(); while(temp[index] != null) index++; } input.closeFile(); for (int k = 0; k < index-1; k++) output.writelnString(temp[k]); output.writelnString("Java 1412, line 1"); output.writelnString("Java 1412, line 2"); output.closeFile(); Here is the problem. Constructing a new file object for output destroys the existing file. In this program a file is opened for output before any data is stored in a temporary array. After
40
Before After // Java1413.java
// This program updates an existing file correctly. // The secret is that the data information is stored before the file object for output is constructed. import java.io.*; public class Java1413 { public static void main (String args[]) throws IOException System.out.println("Java1413.java\n\n"); ExpoInFile input = new ExpoInFile("Java1413.txt"); String temp[] = new String[1000]; int index = 0; String inString = input.readString(); while(inString != null) temp[index] = inString; index++; inString = input.readString(); } input.closeFile(); ExpoOutFile output = new ExpoOutFile("Java1413.txt"); for (int k = 0; k < index; k++) output.writelnString(temp[k]); output.writelnString("Java 1413, line 1"); output.writelnString("Java 1413, line 2"); output.closeFile(); Before After
41
What happens if you keep running the program again and again?
Notice how the same 2 lines are added after each successive execution. 2nd Run 3rd Run 4th Run
42
Section 14.7 Using Text Files with Graphics
43
// when working with applets. public class Java1414 extends Applet {
// Java1414.java // This program tries to read in the first 6 lines of line from the file "Java1413.txt" and display // them on an Applet Window. This will not work because "Exceptions" cannot simply be "thrown" // when working with applets. public class Java1414 extends Applet { String line1, line2, line3, line4, line5, line6; public void init() throws IOException ExpoInFile file = new ExpoInFile("Java1413.txt"); line1 = file.readString(); line2 = file.readString(); line3 = file.readString(); line4 = file.readString(); line5 = file.readString(); line6 = file.readString(); file.closeFile(); } public void paint(Graphics g) Expo.setFont(g,"Algerian",Font.BOLD+Font.ITALIC,72); Expo.drawString(g,line1,20,100); Expo.drawString(g,line2,20,200); Expo.drawString(g,line3,20,300); Expo.drawString(g,line4,20,400); Expo.drawString(g,line5,20,500); Expo.drawString(g,line6,20,600);
44
// This program is able to read data from a file and display it on a
// Java1415.java // This program is able to read data from a file and display it on a // graphics screen because it is an "application" and not an "applet". // Since this is an application you compile and execute the same file. import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class Java1415 { public static void main(String args[]) throws IOException GfxApp gfx = new GfxApp(); gfx.setSize(1000,650); gfx.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); gfx.readFile(); gfx.show(); }
45
class GfxApp extends Frame {
String line1, line2, line3, line4, line5, line6; public void paint(Graphics g) Expo.setFont(g,"Algerian",Font.BOLD+Font.ITALIC,72); Expo.drawString(g,line1,20,100); Expo.drawString(g,line2,20,200); Expo.drawString(g,line3,20,300); Expo.drawString(g,line4,20,400); Expo.drawString(g,line5,20,500); Expo.drawString(g,line6,20,600); } public void readFile() throws IOException ExpoInFile file = new ExpoInFile("Java1413.txt"); line1 = file.readString(); line2 = file.readString(); line3 = file.readString(); line4 = file.readString(); line5 = file.readString(); line6 = file.readString(); file.closeFile();
46
Java1415 – Output
47
Java1416 – Data File
48
// Java1416.java // This program reads 19 strings from a file, converts them to integers, // and them uses the int values to draw 4 circles on the graphics screen. import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class Java1416 { public static void main(String args[]) throws IOException GfxApp gfx = new GfxApp(); gfx.setSize(1000,650); gfx.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); gfx.readFile(); gfx.show(); }
49
class GfxApp extends Frame
{ int x1, x2, x3, x4, x5, x6; int y1, y2, y3, y4, y5, y6; int r1, r2, r3, r4, r5, r6, r7; public void paint(Graphics g) Expo.drawCircle(g,x1,y1,r1); Expo.drawCircle(g,x2,y2,r2); Expo.drawCircle(g,x3,y3,r3); Expo.fillCircle(g,x4,y4,r4); Expo.fillCircle(g,x5,y5,r5); Expo.drawOval(g,x6,y6,r6,r7); }
50
public void readFile() throws IOException
{ ExpoInFile file = new ExpoInFile("Java1416.txt"); x1 = Integer.parseInt(file.readString()); y1 = Integer.parseInt(file.readString()); r1 = Integer.parseInt(file.readString()); x2 = Integer.parseInt(file.readString()); y2 = Integer.parseInt(file.readString()); r2 = Integer.parseInt(file.readString()); x3 = Integer.parseInt(file.readString()); y3 = Integer.parseInt(file.readString()); r3 = Integer.parseInt(file.readString()); x4 = Integer.parseInt(file.readString()); y4 = Integer.parseInt(file.readString()); r4 = Integer.parseInt(file.readString()); x5 = Integer.parseInt(file.readString()); y5 = Integer.parseInt(file.readString()); r5 = Integer.parseInt(file.readString()); x6 = Integer.parseInt(file.readString()); y6 = Integer.parseInt(file.readString()); r6 = Integer.parseInt(file.readString()); r7 = Integer.parseInt(file.readString()); file.closeFile(); }
51
Java1416 – Output
52
Java1417 Data Files
53
// Java1417.java // This program is taken from one of the assignments in Advanced Graphics. // The program reads text from a file whose name is entered by the user. // Each character in the file is tied to a specific graphics image. // By manipulating the text files, you can manipulate the background. // This program also shows that it is possible for an applet to read // (not write) information from a file. Keep in mind that this program // demonstrated features more advanced that what is covered in PreAP Computer Science. // The object here is not a complete understanding of the code. // The object is simply to demonstrate what is possible. // When you execute this program, enter one of these file names: // DK1.dat, DK2.dat, DK3.dat, Expo.dat or blank.dat import java.io.*; import java.awt.*; import java.applet.*; import javax.swing.JOptionPane; public class Java1417 extends Applet { int numRows = 35; int numCols = 50; String background[]; String fileName;
54
public void init() { fileName = JOptionPane.showInputDialog("Enter file name for graphics background."); background = new String[numRows]; try BufferedReader inStream = new BufferedReader(new FileReader(fileName)); String line; int row = 0; while((line = inStream.readLine()) != null) background[row] = line; row++; } inStream.close(); catch (IOException e) System.out.println("There were problems with the code as stated below\n"); System.out.println(e.getMessage()); System.out.println();
55
public void paint(Graphics g)
{ for (int r = 0; r < numRows; r++) for (int c = 0; c < numCols; c++) switch(background[r].charAt(c)) case '.' : drawSpace(g,r,c); break; case '=' : drawGirder(g,r,c); break; case '#' : drawLadder(g,r,c); break; case 'H' : drawHammer(g,r,c); break; case 'B' : drawBarrel(g,r,c); break; case '*' : drawLock(g,r,c); break; case '|' : drawPole(g,r,c); break; default : drawUnknown(g,r,c); } The remainder of this program will not be shown at this time.
56
Java1417 – Output #1
57
Java1417 – Output #2
58
Java1417 – Output #3
59
Java1417 – Output #4
60
Java1417 – Output #5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.