Download Slides14 - Vernon Computer Science

Document related concepts
no text concepts found
Transcript
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 one 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 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.
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.
File Definition
A file is a sequence of information
stored as bytes on a disk, tape, CD
or other external storage device.
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.
Sequential Access
Examples
Random Access Examples
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:
.doc files require Word
.xls files require Excel
.ppt files require PowerPoint
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
The
file structuremethod
is constructed
with
Data.txt
. from
//
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.
// The <readString> method transfers an individual string from
// an external hard drive file to internal file object.
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.
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());
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.
// 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");
}
}
// 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");
}
}
// 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");
}
}
// 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");
}
}
// 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.
// 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?
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
{
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");
}
}
// 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");
}
}
// 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");
}
}
// 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);
inString = input.readString();
}
input.closeFile();
output.closeFile();
}
}
// 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();
}
}
// 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();
}
}
// 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();
}
}
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 files cannot be used 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 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.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Java1417
{
public static void main(String args[]) throws IOException
{
GfxApp gfx = new GfxApp();
gfx.setSize(1024,768);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.readFile();
gfx.show();
}
}
class GfxApp extends Frame
{
int numRows = 36; // 35 Rows are displayed.
int numCols = 50;
String background[];
String fileName;
The top row (row 0) it hidden behind the title bar.
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;
default : drawUnknown(g,r,c);
}
}
public void readFile() throws IOException
{
fileName = JOptionPane.showInputDialog(
"Enter file name for graphics background.");
background = new String[numRows];
ExpoInFile file = new ExpoInFile(fileName);
int row = 0;
String line = file.readString();
while(line != null)
{
background[row] = line;
row++;
line = file.readString();
}
file.closeFile();
}
The remainder of the program goes beyond the scope of this
first year course and will not be discussed at this time.
Java1417 – Output #1
Java1417 – Blank.dat
Java1417 – Output #2
Java1417 – Expo.dat
Java1417 – Output #3
Java1417 – DK.dat