Download presentation
Presentation is loading. Please wait.
Published byOctavia Fowler Modified over 8 years ago
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 This is the fundamental building block of all computer information – a binary digit – which stores only a 1 or 0. byte One byte equals 8 bits. The ASCII format uses 1 byte to store a character. The Unicode format uses 2 bytes to store a character. field A field is one specific unit of information. Examples: name, address, gpa, salary, diagnosis record A record consists of a set of fields for a specific purpose. Examples of the fields contained by different records: student record: name, address, gpa, classRank employee record: name, address, salary, position patient record: name, address, diagnosis, allergies file A data base file consists of a sequence of records. Example: A school can have a file of student records.
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: Input and 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.
6
File Data Structure Definition – Review A file data structure is an internal data structure - with an unspecified number of elements of the same type - assigned to an external file name. The file data structure allows transfer of data between internal and external storage.
7
External File Definition An external file is a sequence of information stored as bytes on a hard drive, disk, tape, CD, DVD, jump drive, or some other external storage device.
8
Text Files and Binary Files Text files contain only characters and can be edited by any text editor like Notepad or jGRASP. All Java programs are text files. Binary files have a specific format which requires a specific application for editing. Examples:.docx files require Word.pptx files require PowerPoint.xlsx files require Excel.accdb files require Access Data Base
9
2 Ways to Classify Files by what they store (text vs. binary) by how the information is accessed (sequential access vs. random access)
10
Sequential Access & 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.
11
Sequential Access Examples
12
Random Access Examples
13
NOTE:
15
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.
16
File Buffers 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.
17
// Java1401.java // This first program example creates an object. // The 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"); }
18
// Java1401.java // This first program example creates an object. // The 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.
19
// Java1401.java // This first program example creates an object. // The 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.
20
// Java1401.java // This first program example creates an object. // The 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.
21
// Java1401.java // This first program example creates an object. // The 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.
22
// Java1401.java // This first program example creates an object. // The 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. JAVA1401.JAVA DATA.TXT FILE
23
// Java1401.java // This first program example creates an object. // The 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.
25
// 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"); } JAVA1402.JAVA DATA.TXT FILE This file is created to test the class. A file like this can be created with any text editor
26
// Java1403.java // This program uses a 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 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"); } JAVA1403.JAVA DATA.TXT FILE This file is created to test the class. A file like this can be created with any text editor like Notepad or jGRASP. A text file should not be created with a word processor since the special formatting characters will cause confusion. null
27
// 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"); } JAVA1404.JAVA DATA.TXT FILE This file is created to test the class. A file like this can be created with any text editor like Notepad or jGRASP. A text file should not be created with a word processor since the special formatting characters will cause confusion. null
28
// Java1405.java This program reads in the text file correctly. // The call before the while loop and the proper placement // of the 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"); } JAVA1405.JAVA DATA.TXT FILE This file is created to test the class. A file like this can be created with any text editor like Notepad or jGRASP. A text file should not be created with a word processor since the special formatting characters will cause confusion.
29
// 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 jGRASP 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"); }
30
JAVA1406.JAVA // 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 jGRASP 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"); }
32
// Java1407.java // This program example creates an object. // The 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"); }
33
// Java1407.java // This program example creates an object. // The 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"); } Java1407.java Java1407.txt file created
34
// Java1407.java // This program example creates an object. // The 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? Java1407.java Java1407.txt file created
35
// Java1407.java // This program example creates an object. // The 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! Java1407.java Java1407.txt file created
36
// Java1408.java This program transfers five strings from internal memory // to an external hard drive file with the method. // The 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"); } Java1408.java Java1408.txt file created
37
// Java1409.java This program transfers five strings from internal memory // to an external hard drive file with the method. // The 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"); } Java1409.java Java1409.txt file created
39
// Java1410.java // This program creates two file objects in the same program. // The object is constructed for file reading transfer. // The 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"); String inString = input.readString(); while(inString != null) { output.writelnString(inString); inString = input.readString(); } input.closeFile(); output.closeFile(); } OriginalCopy Java1410.java
41
// 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(); } BeforeAfter Java1411.java
42
// Java1412.java This program opens up one file for input and another file for output. // The intent 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++; temp[index] = input.readString(); } 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 Java1412.java
43
// 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++; temp[index] = input.readString(); } 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
44
// 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 Java1413.java
45
What happens if you keep running the program again and again? Notice how the same 2 lines are added after each successive execution. 3 rd Run2 nd Run4 th Run
47
// 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); }
48
// 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(); }
49
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(); }
50
Java1415 – Output
51
Java1416 – Data File
52
// Java1416.java // This program reads 19 strings from a file, converts them to integers, // and them uses the 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(); }
53
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); }
54
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(); }
55
Java1416 – Output
56
Java1417 Data Files
57
// 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 Pre-AP ® 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;
58
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(); } This init method demonstrates how to read data from a text file in an applet. This requires Exception Handling which is beyond the scope of this course. For now, it is good that we at least know it is possible.
59
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. For now, we want to focus specifically on this method.
60
Java1417 – Output #1
61
Java1417 – Output #2
62
Java1417 – Output #3
63
Java1417 – Output #4
64
Java1417 – Output #5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.