Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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. 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. 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. 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. 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. 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 2 Ways to Classify Files • by what they store (text vs. binary) • by how the information is accessed (sequential access vs. random access) 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. Sequential Access Examples Random Access Examples NOTE: 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. 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. // 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 { System.out.println("\nJAVA1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = f.readString(); System.out.println(inString); f.closeFile(); System.out.println("\n\n"); } } // #1 // #2 // #3 // #4 // #5 // #6 // Java1401.java // This first program example creates an <ExpoInfile> object. Line #1 uses a Java standard input/output library, // The <readString> method transfers an individual string from which provides access to classes for file handling. // an external hard drive file to internal file object. import java.io.*; public class Java1401 { public static void main (String args[]) throws IOException { System.out.println("\nJAVA1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = f.readString(); System.out.println(inString); f.closeFile(); System.out.println("\n\n"); } } // #1 // #2 // #3 // #4 // #5 // #6 // Java1401.java // This #2 firstadds program example creates an <ExpoInfile> object. Line the keywords throws IOException. // The <readString> method transfers an individual string from Java wants to know how the programmer, // an external hard drive fileyou, to internal file object. will deal with potential IO problems. We literally tell Java to throw away or import java.io.*; ignore any input/output errors that may occur. public class Java1401 { public static void main (String args[]) throws IOException { System.out.println("\nJAVA1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = f.readString(); System.out.println(inString); f.closeFile(); System.out.println("\n\n"); } } // #2 // #3 // #4 // #5 // #6 // Java1401.java // This#3 first program an <ExpoInfile> object. Line creates an fexample object creates of the ExpoInFile class. // Thef <readString> transfers an individual string from The file structuremethod is constructed with Data.txt. // an external hardinternal drive file to object internalf file object. This means the file is associated with the external text file Data.txt. The ExpoInFile class contains import java.io.*; // #1 methods for the transfer of data from external storage to internal memory. This is known as opening a file for input. 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"); } } // Java1401.java // This#4 first program example creates an object. Line gets down to the business of<ExpoInfile> actually transferring // Thestring <readString> method transfers an individual string memory from one of data from the Data.txt file to internal // an external hard drive file to internal file object. with the readString method. The single line of characters is stored the memory location of the inString variable. importinjava.io.*; public class Java1401 { public static void main (String args[]) throws IOException { System.out.println("\nJAVA1401.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); String inString = f.readString(); System.out.println(inString); f.closeFile(); System.out.println("\n\n"); } } // #2 // #3 // #4 // #5 // #6 // Java1401.java // This first program example creates an <ExpoInfile> object. JAVA1401.JAVA // The <readString> method transfers an individual string from // an external hard drive file to internal file object. DATA.TXT FILE import java.io.*; // 1 public Java1401 the value of the inString variable. Line class #5 displays { This letsstatic youvoid seemain the(String first line ofthrows the Data.txt file.// #2 public args[]) IOException { 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"); } } // Java1401.java // This#6 first program example creates anby <ExpoInfile> object. Line concludes the file handling // The <readString> method transfers an individual string from closing the file object. Failure to close a // anobject external hard driveinfile to internal file can result data loss. file object. Think of it as closing the chicken coop import java.io.* after you pick up the eggs. 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"); } } // Java1402.java // This program demonstrates how to read in four lines from // the external "Data.txt" file. JAVA1402.JAVA import java.io.*; DATA.TXT public classFILE Java1402 { This filestatic is created testargs[]) the throws <ExpoInFile> class. public void main to (String IOException A file like this can be created with any text editor { System.out.println("\nJAVA1402.JAVA\n\n"); ExpoInFile f = new ExpoInFile("Data.txt"); System.out.println(f.readString()); System.out.println(f.readString()); System.out.println(f.readString()); System.out.println(f.readString()); f.closeFile(); System.out.println("\n\n"); } } // Java1403.java // This program uses a <for> loop to read in file data. JAVA1403.JAVA // A fixed loop structure can cause problems, because the precise file size // might not be known. The last couple of <null> outputs indicate that DATA.TXT FILE // there was no more string data in the external text file. This file is created to test the <ExpoInFile> class. import java.io.*; A file like this can be created with any text editor like Notepad or jGRASP. public class Java1403 { text file should not be created with a word processor A since the special formatting characters cause confusion. public static void main (String args[])will throws IOException null null{ 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"); } } // Java1404.java 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. DATA.TXT FILE import java.io.*; This file is created to test the <ExpoInFile> class. public A file class likeJava1404 this can be created with any text editor {like Notepad or jGRASP. public static void main (String args[]) throws IOException A text file should not be created with a word processor { since the special formatting characters will cause confusion. System.out.println("\nJava1404.JAVA\n\n"); null 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"); } } // Java1405.java This program reads in the text file correctly. JAVA1405.JAVA // The <readString> call before the while loop and the proper placement // of the <readString> call inside the loop body cures the problem. DATA.TXT FILE import java.io.*; This created to test the <ExpoInFile> class. publicfile class is Java1405 A { file like this can be created with any text editor like Notepad or jGRASP. public static void main (String args[]) throws IOException { file should not be created with a word processor A text System.out.println("\nJava1405.JAVA\n\n"); since the special formatting characters will cause confusion. 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"); } } // 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"); } } 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"); } } // 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"); } } // Java1407.java // This program example creates an <ExpoOutfile> object. Java1407.java // The <writeString> method transfers an individual string from internal // file object to external hard drive file. Java1407.txt file created 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 // This program example creates an <ExpoOutfile> object. Java1407.java // The <writeString> method transfers an individual string from internal // file object to external hard drive file. Where is the output of import this java.io.*; line? Java1407.txt file created 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 // 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.*; Here it is! public class Java1407 Java1407.java { public static void main (String args[]) throws IOException { Java1407.txt file created 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"); } } // 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 "); Java1408.java f.writeString("Line-2 "); f.writeString("Line-3 "); f.writeString("Line-4 "); Java1408.txt file created f.writeString("Line-5 "); f.closeFile(); System.out.println("Java1408.txt file created\n\n"); } } // Java1409.java This program transfers five strings from internal memory // to an external hard drive file with the <writelnString> method. Java1409.java // The <writelnString> method does provide a line feed. import java.io.*; public class Java1409 file created Java1409.txt { 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"); } } // 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"); String inString = input.readString(); while(inString != null) { output.writelnString(inString); inString = input.readString(); } input.closeFile(); output.closeFile(); } } Java1410.java // 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(); } Java1411.java } // 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"); Java1412.java 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(); } } // 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"); 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. 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(); } } // 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(); } } Java1413.java Before After What happens if you keep running the program again and again? Notice how the same 2 lines are added after each successive execution. // 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); } } // 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(); } } 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(); } } Java1415 – Output Java1416 – Data File // 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(); } } 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); } 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(); } } Java1416 – Output Java1417 Data Files // 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; 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) { This init method demonstrates how background[row] = line; to read data from a text file in an row++; } applet. This requires Exception inStream.close(); Handling which is beyond the scope } of this course. For now, it is good catch (IOException e) that we at least know it is possible. { System.out.println("There were problems with the code as stated below\n"); System.out.println(e.getMessage()); } System.out.println(); } 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. Java1417 – Output #1 Java1417 – Output #2 Java1417 – Output #3 Java1417 – Output #4 Java1417 – Output #5