Java
  Home arrow Java arrow Page 4 - Working with Text Files and File Name Filt...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Working with Text Files and File Name Filters in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-04-27

    Table of Contents:
  • Working with Text Files and File Name Filters in Java
  • Writing Text Files
  • Files and Filename Filters
  • Summary
  • Quiz

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 days,"...
     

    Buy this book now. 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.

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek