Thisclass simply consists three public fields title
|
|
---|
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 |
---|
|
---|
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;
|
Book VIII Chapter 2 |
---|
➝5 | The main method begins by calling a method named getMovies, | |
|
||
➝15 |
|
|
➝43 | The openWriter method creates a PrintWriter object for the | |
➝61 | The writeMovie method accepts as parameters a Movie object | |
➝70 | ||
|
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 |
|
---|
|
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)
|
Book VIII | |
|
Chapter 2 | |
Reads a byte value from the input | ||
stream. It throws EOFException | ||
|
Reads a char value from the input | |
stream. It throws EOFException | ||
|
|
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.