Working with Text Files and File Name Filters in Java - Summary
(Page 4 of 5 )
Today you learned how to work with streams in two directions: pulling data into a program over an input stream and sending data from a program using an output stream.
You used character streams to handle text and byte streams for any other kind of data. Filters were associated with streams to alter the way information was delivered through a stream, or to alter the information itself.
Today's lesson covers most java.io package classes, but there are other types of streams you might want to explore. Piped streams are useful when communicating data among different threads, and byte array streams can connect programs to a computer's memory.
Because the stream classes in Java are so closely coordinated, you already possess most of the knowledge you need to use these other types of streams. The constructors, read methods, and write methods are largely identical.
Streams are a powerful way to extend the functionality of your Java programs because they offer a connection to any kind of data you might want to work with.
Tomorrow you will use streams to read and write Java objects.
Q&A
Q. A C program that I use creates a file of integers and other data. Can I read this using a Java program?
A. You can, but one thing you have to consider is whether your C program represents integers in the same manner that a Java program represents them. As you might recall, all data can be represented as an individual byte or a series of bytes. An integer is represented in Java using four bytes arranged in what is called big-endian order. You can determine the integer value by combining the bytes from left-to-right. A C program implemented on an Intel PC is likely to represent integers in little-endian order, which means that the bytes must be arranged from right-to-left to determine the result. You might have to learn about advanced techniques, such as bit shifting, to use a data file created with a programming language other than Java.
Q. Can relative paths be used when specifying the name of a file in Java?
A. Relative paths are determined according to the current user folder, which is stored in the system properties user.dir. You can find out the full path to this folder by using the System class in the main java.lang package, which does not need to be imported.
Call the System class method getProperty(String) method with the name of the property to retrieve, as in this example:
String userFolder = System.getProperty("user.dir");
The method returns the path as a string.
Q. The FileWriter class has a write(int) method that's used to send a character to a file. Shouldn't this be write(char)?
A. The char and int data types are interchangeable in many ways; you can use an int in a method that expects a char, and vice versa. This is possible because each character is represented by a numeric code that is an integer value. When you call the write() method with an int, it outputs the character associated with that integer value. When calling the write() method, you can cast an int value to a char to ensure that it's being used as you intended.
Next: Quiz >>
More Java Articles
More By Sams Publishing
|
This article 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). Check it out today at your favorite bookstore. Buy this book now.
|
|