Working with Text Files and File Name Filters in Java
This article, the third of three parts, will teach you how the programs you create in Java can interact with different storage devices using a communications system called streams. It is excerpted from chapter 15 of the book Sams Teach Yourself Java 2 in 21 days, written by Roger Cadenhead and Laura Lemay (Sams, ISBN: 0672326280)
Working with Text Files and File Name Filters in Java - Quiz (Page 5 of 5 )
Review today's material by taking this three-question quiz.
Questions
What happens when you create a FileOutputStream using a reference to an existing file?
An exception is thrown.
The data you write to the stream are appended to the existing file.
The existing file is replaced with the data you write to the stream.
What two primitive types are interchangeable when you're working with streams?
byte and boolean
char and int
byte and char
In Java, what is the maximum value of a byte variable and the maximum value of an unsigned byte in a stream?
Both are 255
Both are 127
127 for a byte variable and 255 for an unsigned byte
Answers
c. That's one of the things to look out for when using output streams; you can easily wipe out existing files.
b. Because a char is represented internally by Java as an integer value, you can often use the two interchangeably in method calls and other statements.
c. The byte primitive data type has values ranging from -128 to 127, whereas an unsigned byte can range from 0 to 255.
Certification Practice
The following question is the kind of thing you could expect to be asked on a Java programming certification test. Answer it without looking at today's material or using the Java compiler to test the code.
Given:
import java.io.*;
public class Unknown {
public static void main(String[] arguments) {
String command = "";
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
command = br.readLine();
}
catch (IOException e) { }
}
}
Will this program successfully store a line of console input in the String object named command?
Yes.
No, because a buffered input stream is required to read console input.
No, because it won't compile successfully.
No, because it reads more than one line of console input.
The answer is available on the book's Web site at http://www.java21days.com. Visit the Day 15 page and click the Certification Practice link.
Exercises
To extend your knowledge of the subjects covered today, try the following exercises:
Write a modified version of the HexRead program from Day 7, "Threads, Exceptions, and Assertions," that reads two-digit hexadecimal sequences from a text file and displays their decimal equivalents.
Write a program that reads a file to determine the number of bytes it contains and then overwrites all those bytes with zeroes (0). (For obvious reasons, don't test this program on any file you intend to keep; the data in the file will be wiped out.)
Where applicable, exercise solutions are offered on the book's Web site at http://www.java21days.com.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.