Home arrow Java arrow Page 5 - Working with Text Files and File Name Filters in Java
JAVA

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)

Author Info:
By: Sams Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
April 27, 2006
TABLE OF CONTENTS:
  1. · Working with Text Files and File Name Filters in Java
  2. · Writing Text Files
  3. · Files and Filename Filters
  4. · Summary
  5. · Quiz

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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

  1. What happens when you create a FileOutputStream using a reference to an existing file?

  1. An exception is thrown.

  2. The data you write to the stream are appended to the existing file.

  3. The existing file is replaced with the data you write to the stream.

  1. What two primitive types are interchangeable when you're working with streams?

  1. byte and boolean

  2. char and int

  3. byte and char

  1. In Java, what is the maximum value of a byte variable and the maximum value of an unsigned byte in a stream?

  1. Both are 255

  2. Both are 127

  3. 127 for a byte variable and 255 for an unsigned byte

Answers

  1. c. That's one of the things to look out for when using output streams; you can easily wipe out existing files.

  2. 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.

  3. 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?

  1. Yes.

  2. No, because a buffered input stream is required to read console input.

  3. No, because it won't compile successfully.

  4. 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:

  1. 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.

  2. 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.

blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials