Welcome to Lesson 20!


Learning Objectives
By the end of today's class, you should know...
  • How do you declare an array?
  • How do you assign it values?
  • What is static initialization?
  • How do you read from and write to a file in your program?

Announcements

  • Final Project Presentations next week
    • See me at break if you did not get on a team
  • Lab 10 due Friday
  • Quiz 7 after the break


Review Activity

1. Write the following methods:

Method 1
  • The method is named printGrade
  • It takes in a double for the score
  • It prints out the grade to the console as A, B, C, D, or F with a message, "The grade is: <grade>"
  • It returns nothing

Method 2

  • The method is named convertGrade
  • It takes in a double for the score
  • It determines if the grade is A, B, C, D, or F
  • It returns a char for the grade


Using the starter code below, call the above printGrade and convertGrade methods:


public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double score;
    char grade;
    System.out.println("Please enter your score: ");
    score = input.nextDouble();
   
    //call printGrade here
    //Note: with printGrade, I can only display the grade to the console

    //call convertGrade here
    System.out.println("Your grade is " + grade);
    //Note: with convertGrade, I can store the grade
    //and do more with it than display it. It is
    //stored for future use.

}


File I/O


About Program I/O

  • Program I/O = Program Input/Output
    • Input to and output from programs
  • Input can be from a keyboard, mouse or file
  • Output can be to a display screen, printer or file
  • Note that files can be both input and output devices for programs
  • Advantages of file I/O:
    • Data still exists after the program ends
    • Input can be automated (rather than entered manually)
    • Output from one program can be input to another
  • To store and retrieve data in a file, we need two items:
    • A file
    • A file stream object
  • We will look at files first


Files

File: a collection of data stored under a common name on a storage medium.

  • Files provide long-term storage of large amounts of data
  • Usually, you store files on non-volatile storage mediums
    • Magnetic disks
    • Optical disks
    • Flash storage, like USB storage devices
  • File are a single sequence of bytes

    Byte 0 Byte 1 Byte 2 ... Byte n−1 End-of-file marker

  • The operating system keeps track of the number of bytes in a file
  • Files must have a name
    • Naming requirements depend on the underlying operating system (OS)
  • The operating system organizes files into directories


Types of Files

  • All data in a file is ultimately just zeros and ones
  • It is up to the program using the file to understand the meaning and internal format of the data
  • In general, programs interpret data using two broad categories: text and binary


Text Files

  • In text files, the bits represent printable characters
  • Files are usually stored as one byte per character (ASCII)
  • Each line is delimited by end-of-line character
  • You can read text files because each byte is interpreted by a program as textual characters
  • Some of these programs, like NotePad, then display the textual data to your computer's screen
  • Since there are many programs that read and display text, text files are called human readable


Binary Files

  • Data other than text is usually referred to as binary data
  • Each bit represents some type of encoded information like program instructions or integer data
  • Binary files are easily read by the computer but not by humans
  • The following table compares binary and text values saved in a file
  • First we consider the value "1234" as ASCII codes and compare these bits to a binary value of 1234


Comparing Binary and Textual Data

Description Byte 0 Byte 1 Byte 2 Byte 3
"1234" as char's '1' '2' '3' '4'
"1234" as ASCII codes (bytes) 49 50 51 52
"1234" as ASCII codes (bits) 00110001 00110010 00110011 00110100
(int) 1234 as binary bits 00000000 00000000 00000100 11010010


Creating File Objects

  • File class represents a file on the native file system
  • You create a File object referring to a file
  • Then you can query the object for information about that file

Common Constructors for Class File

Class Description
File(String pathname) Creates a File object that refers to the specified pathname.
File(String parent, String child) Creates a File object that refers to the pathname created by combining the parent and child pathnames.
File(File parent, String child) Creates a File object that refers to the pathname created by combining the pathname of parent with the child pathname.

Examples of Creating Files Objects

  • You can create a File object in the current directory:
    File myFile = new File("myfile.txt");
  • Can create a File object using an absolute pathname
  • Specifies the entire path and file name for the file:
    File myFile = new File("C:/docs/myfile.txt");

Using File Objects

  • We can use File objects for many tasks
  • For instance we can:
    • Test some attributes of files or directories
    • Get information about files
    • Get lists of directory contents
    • Create files and directories
    • Delete files and directories
  • You can see some of the commonly used methods listed below

Some Methods that Test a File

Method Description
canRead() Returns true if the pathname exists and can be read by the program.
canWrite() Returns true if the pathname exists and a program can write to it.
exists() Returns true if the pathname exists.
isDirectory() Returns true if the pathname exists and refers to a directory.
isFile() Returns true if the pathname exists and refers to a file.

Some Methods that Return Information about a File

Method Description
getName() Returns the name of the file or directory as a String.
getPath() Returns the name of the file or directory as a String.
lastModified() Returns the time that the file was last modified as the number of milliseconds since January 1, 1970.
length() Returns the length of the file as a long type.

Some Methods that work with Directories

Method Description
list() If the object refers to a directory, returns an array of strings naming the files and directories.
listFiles() If the object refers to a directory, returns an array of File objects for the files and directories.
listRoots() A static method that returns an array of File objects representing the drives available to the current system.
mkdir() Creates the directory named by the File pathname.

Other Methods that work with File Objects

Method Description
createNewFile() Creates a new, empty file if a file with this name does not yet exist.
delete() Deletes the file or directory referred to by the File object.
setReadOnly() Makes the file read-only. If successful, returns true.



Working with Files and Directories

  • The following programs show some uses for the File object

Getting Information About a File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;

public class FileInfo {
  public static void main(String[] args) throws IOException {
    File myFile = new File("myfile.txt");
    myFile.createNewFile();
    if (myFile.exists()) {
        System.out.println("Name:     " + myFile.getName());
        System.out.println("Path:     " + myFile.getPath());
        System.out.println("Writable: " + myFile.canWrite());
    } else
        System.out.println("The " + myFile.getName()
                + " file doesn't exist.");
  }
}

Streams

Stream: a one-way transmission path that either delivers data to a destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.)

  • A stream connects a program to an I/O object
  • Input stream: an object that provides a sequence of bytes to a program

  • Output stream: an object that accepts a sequence of bytes from a program

File Streams

File stream: a one-way transmission path used to connect a program to a file.

  • File streams can be either input or output streams
  • File input streams receive data from a file
  • File output streams send data to a file
  • Each file your program uses will need a separate file stream object

Basic File Streams

  • Since there are two types of files, there are streams to support each type
  • Use character streams to read and write text files:
  • Use binary streams to read and write binary files:

Text Streams

  • Streams are implemented using classes and objects - a topic you will cover in more detail next quarter
  • To write and read text, you use character streams
  • A character stream processes characters through the stream
  • There is a separate set of classes for writing and reading character streams. In this class, we will be using:
    • PrintWriter: Connects a stream to a file for writing data from the program to the file
    • Scanner: Connects a stream to a file for reading data from a file into the program

Writing to a Text Stream

  • To write text to a file, we use a PrintWriter stream:
    PrintWriter output = new PrintWriter("out.txt"); 
Example program to write out data to a file:

import java.io.*;
public class Files {
    public static void main(String[] args) throws IOException{
    
        File file = new File("scores.txt");
        PrintWriter output = new PrintWriter(file);
       
        output.print("100, 95, 90, 85");
        output.close();
       
    }
   
}



Connecting a Character Output Stream to a File

  • To write to a text file, you create a character output stream
  • To do this, you need to layer two or more classes from the Writer hierarchy
  • An example of how to accomplish this task is shown below

Procedure for Writing a Text File
  1. Place import java.io.*; at the top of your program
    1. Note that .* is the wildcard. Using a * will import all classes from that package.
    2. We need 3 classes here so we will save time by using the * instead of importing all 3 separately (java.io.File, java.io.PrintWriter, and java.io.IOException)
  2. Add throws IOException as part of the signature for main to avoid compiler errors.
  3. Create a File object with a pathname to your file:
    File data = new File("data.txt");
    
  4. Connect the file to a stream:
    PrintWriter out = new PrintWriter(data);
    

    If you want to append data then use:

    PrintWriter out = new PrintWriter(data, true);
    
  5. Write to the file using the print() and println() methods:
    out.println("This is a string");
    out.println('c');
    out.println(1234);
    out.println(1.234);
    for (int i = 0; i <= 10; i++) {
        out.print(i + ",");
    }
    out.println();
    
  6. Close the streams when finished writing:
    out.close();
    


Example Program Writing a Text File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;

class TextWriterApp {
    public static void main(String[] args)
            throws IOException {
        File data = new File("data.txt");
        PrintWriter out = new PrintWriter(data);
        out.println("This is a string");
        out.println('c');
        out.println(1234);
        out.println(1.234);
        for (int i = 0; i <= 10; i++) {
            out.print(i + " ");
        }
        out.println();
        out.close();
        System.out.println("Finished writing file");
    }
}


Connecting a Character Input Stream to a File
  • To read from a text file, you need to create a character input stream
  • We can use the same Scanner object we used for reading from the keyboard
  • An example of how to accomplish this task is shown below

Classes Connecting a Character Output Stream to a File

import java.io.*;
import java.util.Scanner;
public class Files {
public static void main(String[] args) throws IOException{

File file = new File("scores.txt");
Scanner input = new Scanner(file);
int score = input.nextInt();
System.out.println("Score " + score);
input.close();
}
}

Some Commonly Used Methods of a Scanner Object

Method Description
next() Returns the next token as a String object.
nextLine() Returns the rest of the current line as a String object.
nextDouble() Provides classes to handle text, dates, and numbers
nextInt() Returns the next token as an int value.


Procedure For Reading a Text File
  1. Place import java.io.*; at the top of your program
    1. Note that .* is the wildcard. Using a * will import all classes from that package.
  2. Add throws IOException as part of the signature for main to avoid compiler errors.
  3. Create a File object with a pathname to your file:
    File data = new File("data.txt");
    
  4. Add a Scanner object to parse the data in the file:
    Scanner input = new Scanner(data);
    
  5. Read data using methods calls for the correct type:
    int x = input.nextInt();
    
  6. Close the streams when finished reading:
    in.close();
    

Note that you can nest the constructor calls as shown in the example below

Example Program Reading a Text File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

import java.io.*;
import java.util.Scanner;

class TextReaderApp {
    public static void main(String[] args)
            throws IOException {
        File data = new File("data.txt");
        Scanner input = new Scanner(data);
        String str = input.nextLine();
        System.out.println(str);

        char ch = input.nextLine().charAt(0);
        System.out.println(ch);

        int x = input.nextInt();
        System.out.println(x);

        double d = input.nextDouble();
        System.out.println(d);

        for (int i = 0; i <= 10; i++) {
            x = input.nextInt();
            System.out.print(x + " ");
        }
        System.out.println();

        input.close();
    }
}

Activity 20.1: Two Numbers (10 pts)

  • In this exercise we write a program that copies two numbers from an input stream to an output stream.
  • Create a new Java project and name it Files.
  • Then, let's create a text file to test our program.
  • In Eclipse, click on the name of the project. Then, go to File->New->File
  • Name this file infile.txt.
  • In the file, add a list of numbers each on their own line, like this:
10
20

  • We will read from this file after writing our program.

  • Next, create a second file named outfile.txt. You can leave this file blank.
  • We will write out to this file in our program.
  • Place the following import statement above main:
    import java.io.*; 
  • Also, as part of the signature of main(), add throws IOException:
    public static void main(String[] args) throws IOException{
    
  • Add code to create two new Files:
    File inFile = new File("infile.txt");
File outFile = new File ("outfile.txt"); 
  • Add statements to read two numbers from the input stream. For example, here is possible code for reading the first number:
    Scanner input = new Scanner(inFile);
    int first = input.nextInt();
    
  • Add statements to write the two numbers to the output stream. For example, here is possible code for writing the first number:
    PrintWriter output = new PrintWriter(outFile); 
  • output.println("first=" + first);
    
  • Close the streams when finished reading and writing:
    input.close();
    output.close();
    
  • Compile and run your modified program to make sure you made the changes correctly.

    Notice that you do not see any output on the screen for file reading or writing. The output stream wrote the program output to the output file.

  •  Now, open the output file you created and verify that two numbers were copied. Your output file should look like:
    first=10
    second=20
    
  • Save your program source code file to submit to Canvas

Your Files.java File Should Look Similar to the One Below:



Arrays

Defining Arrays

  • An array is a collection of data items all of the same type
  • You declare an array like this:
    dataType[] variableName = new dataType[length];
    
  • You can also declare an array like this:
    dataType variableName[] = new dataType[length];
    
  • Where:
    • dataType: the data type of all the array items
    • variableName: the name you make up for the array
    • length: the number of data items the array can hold
  • For example, the following is the declaration of an array named scores that holds 10 values of type int:
    int[] scores = new int[10];
  • When a program executes this statement, it creates 10 contiguous slots in memory like this:
    scores = 
     
     
     
     
     
     
     
     
     
     
  • Each of the memory slots can hold one data value
  • We can have arrays of any type:
double[] anArrayOfDoubles;
boolean anArrayOfBooleans[];
char[] anArrayOfChars;
String anArrayOfStrings[];
  • Once you define the length of an array, it cannot change length later in the program.
  • When defining an array, you must guess the maximum number of elements you need to store:
    int[] scores = new int[100];
    
  • If you need to know the length of an array, you can always use the length property (more on this below):
          System.out.println(scores.length) //Will print out 100

Initializing Array Items

  • We specify which slot of an array to access with the [] operator:
    scores[4] = 98;
  • The indexes of arrays are numbered starting at 0
  • We can assign a value to an array element any time after it is declared:
    scores[0] = 90;
    scores[1] = 95;
    scores[2] = 87;
    scores[3] = 89;
    scores[4] = 98;
    
  • We can also initialize array elements in the declaration statement:
    • Called static initialization
    • We use a comma-separated list inside curly-braces
  • For example:
    int[] scores = { 90, 95, 87, 89, 98 };
    
  • This produces the same array as in the previous example
  • The compiler computes the size automatically by counting the items in the list
Default Array Values
  • When an array is declared, Java automatically assigns each element a default value: false for booleans, 0 for numerical types

Accessing Array Items

  • To access the slots in an array, we must specify which slot to use with the [] operator
  • For instance:
    scores[4] = 98;
  • The number inside the brackets is called an index or subscript
  • In Java, the slots of an array are numbered starting at 0, as shown below:
    scores = 
     
     
     
     
    98
     
     
     
     
     
    [0]
    [1]
    [2]
    [3]
    [4]
    [5]
    [6]
    [7]
    [8]
    [9]
  • Thus, assignment to the slot with an index of 4 is put into the fifth slot

Using Slots

  • We declared our example array with a data type of int:
    int[] scores = new int[10];
  • Because scores is an array containing int values, we can use a slot, such as scores[4], just like any variable of type int:
    scores[4]++;
    System.out.println(scores[4]);
    
  • This includes using a slot as an argument to a method with a parameter of the same type:
    public static void myMethod(int singleScore){//method body}
    ...
    myMethod(scores[4]); 

Array Length

  • We can always access the length of an array using .length

System.out.println(scores.length);

  • Note that the length of the array is always one less than its last index

Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array

Image source.

Using Arrays to Collect Data Items

  • Note that the index of an array can be any integer value
  • Thus, we can use an integer variable for the index
  • We can use an integer variable with a loop to read data into the array
  • Also, we can display the contents of an array using a loop
  • The following program shows an example of collecting and displaying data items


Example Program Using Arrays to Collect and Display Data Items



/**
*
* @author parrishj
*/
import java.util.Scanner;
public class Arrays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int[] scores = new int[5];
System.out.println("Array elements initialized to 0:");
for (int i =0; i <scores.length; i++){
System.out.println("Index " + i + ": " + scores[i]);
}
System.out.println("\nEnter " + scores.length + " scores:");
for (int i =0; i <scores.length; i++){
scores[i] = input.nextInt();
}
System.out.println("\nYou entered:");
for (int i =0; i <scores.length; i++){
System.out.println("Score " + i + ": " + scores[i]);
}
}

}

Activity 20.2: My First Array (10pts)

  • Open a new Java project in Eclipse and save it as Arrays, and then compile and run the starter program to make sure you copied it correctly.
    package arrays;

    /**
    *
    * @author
    */
    import java.util.Scanner;
    public class Arrays {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    }

    }

  • Inside main(), add a statement to define an array named names that is suitable for holding a list of three (3) names.
  • Compile your code to make sure it has correct syntax.

    If you have problems, ask a classmate or the instructor for help as needed.

  • Assign values to each of the array elements like:
    names[0] = "Abel Ableson";
    
  • Add a for-loop to display all three array values.
  • Compile your code to make sure it has correct syntax.

    If you have problems, ask a classmate or the instructor for help as needed.

  • When you are finished, upload your source code to Canvas.

Wrap Up
  • With a partner, answer the questions from today's learning objectives


Upcoming Assignments


~Have a Good Weekend!~