Home arrow Java arrow Page 2 - 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 - Writing Text Files
(Page 2 of 5 )

The FileWriter class is used to write a character stream to a file. It's a subclass of OutputStreamWriter, which has behavior to convert Unicode character codes to bytes.

There are two FileWriter constructors: FileWriter(String) and FileWriter(String, boolean). The string indicates the name of the file that the character stream will be directed into, which can include a folder path. The optional Boolean argument should equal true if the file is to be appended to an existing text file. As with other stream-writing classes, you must take care not to accidentally overwrite an existing file when you're appending data.

Three methods of FileWriter can be used to write data to a stream:

  • write(int)—Writes a character

  • write(char[], int, int)—Writes characters from the specified character array with the indicated starting point and number of characters written

  • write(String, int, int)—Writes characters from the specified string with the indicated starting point and number of characters written

The following example writes a character stream to a file using the FileWriter class and the write(int) method:

FileWriter letters = new FileWriter("alphabet.txt");
for (int i = 65; i < 91; i++)
letters.write( (char)i );
letters.close();

The close() method is used to close the stream after all characters have been sent to the destination file. The following is the alphabet.txt file produced by this code:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

The BufferedWriter class can be used to write a buffered character stream. This class's objects are created with the BufferedWriter(Writer) or BufferedWriter(Writer, int) constructors. The Writer argument can be any of the character output stream classes, such as FileWriter. The optional second argument is an integer indicating the size of the buffer to use.

BufferedWriter has the same three output methods as FileWriter: write(int), write(char[], int, int), and write(String, int, int).

Another useful output method is newLine(), which sends the preferred end-of-line character (or characters) for the platform being used to run the program.


Tip - The different end-of-line markers can create conversion hassles when transferring files from one operating system to another, such as when a Windows XP user uploads a file to a Web server that's running the Linux operating system. Using newLine() instead of a literal (such as '\n') makes your program more user-friendly across different platforms.


The close() method is called to close the buffered character stream and make sure that all buffered data is sent to the stream's destination.


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials