Language:EN
Pages: 5
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
thisclass simply consists three public fields titl

Thisclass simply consists three public fields title

752

Writing Character Streams

import java.io.*;

public class WriteFile
{

5
15

Movie[] movies = new Movie[10];

movies[0] = new Movie("It's a Wonderful Life", 1946, 14.95);
movies[1] = new Movie("Young Frankenstein", 1974, 16.95);
movies[2] = new Movie("Star Wars",
1977, 17.95);
movies[3] = new Movie("The Princess Bride", 1987, 16.95);
movies[4] = new Movie("Glory",
1989, 14.95);
movies[5] = new Movie("The Game",
1997, 14.95);
movies[6] = new Movie("Shakespeare in Love", 1998, 19.95);
movies[7] = new Movie("Zombieland",
1997, 18.95);
movies[8] = new Movie("The King's Speech", 1997, 19.95);

Writing Character Streams

43

try
{
File file = new File(name);
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter(file) ), true ); return out;
}
catch (IOException e)
{
System.out.println("I/O Error");

}
61
70

public String title;

public int year;
public double price;

Book VIII Chapter 2
5 The main method begins by calling a method named getMovies,

which returns an array of Movie objects to be written to the file. (The Movie class is defined as an inner class later in the program.) Then it calls openWriter, which creates a PrintWriter object that the program can use to write data to the file. Next, it uses an enhanced for loop to call the writeMovie method for each movie in the array. This method accepts a Movie object that contains the movie to be written and a PrintWriter object to write the movie to. Finally, the PrintWriter is closed.

15

The getMovies method returns an array of Movie objects that

43 The openWriter method creates a PrintWriter object for the
61 The writeMovie method accepts as parameters a Movie object
70

class simply consists of three public fields (title, year, and price) and a constructor that initializes the fields.

Reading Binary Streams

BufferedInputStream: This class adds buffering to the basic
FileInputStream, which improves the stream’s efficiency and gives it a moist and chewy texture.

Reading Binary Streams

755

Constructors

Description

BufferedInputStream (InputStream in)

Creates a file input stream from
the specified object. It throws
FileNotFoundException if the file doesn’t exist or if it’s a directory rather than a file.

FileInputStream(String path)

Reads a boolean value

Book VIII

from the input stream. It

Chapter 2
Reads a byte value from the input
stream. It throws EOFException

char readChar()

Reads a char value from the input
stream. It throws EOFException

and IOException.

Constructors

Description

double readDouble()

Reads an int value from the input stream. It throws EOFException and IOException.

long readLong()

Reads a string stored in UTF format from the input stream. It throws
EOFException, IOException, and UTFDataFormatException.

The following sections present programs that read and write data in a binary file named movies.dat that contains information about movies. Each record in this file consists of a UTF string containing the movie’s title, an int repre-senting the year when the movie was released, and a double representing the price I paid for the movie at my local discount video store. Although the format of this file is different from that of the movies.txt file shown earlier in this chapter, the file contains the same data. Refer to the section “Reading Character Streams,” earlier in this chapter, to see a list of the movies in this file.

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Abigail Rodriguez

PageId: DOCF13344E