aspose file tools*
The moose likes I/O and Streams and the fly likes File Writers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File Writers" Watch "File Writers" New topic
Author

File Writers

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I was learning about APIs.
As far as i understood,we can write a file using FileWriter,BufferedWriter and printWriter and i guess also with Writer classes. And read a file using the respective readers.
( Correct me if i am wrong about the above statement)
But i am not sure which one to choose. :roll:
can any of you tell me that,which class to choose and what is that "thing" ( or measure ) which tell me to choose a particular class.

Thankyou


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The I/O (Input/Output) classes can be confusing at first. Have you read the Basic I/O lesson in the Java Tutorial yet?


As far as i understood,we can write a file using FileWriter,BufferedWriter and printWriter and i guess also with Writer classes. And read a file using the respective readers.
( Correct me if i am wrong about the above statement)


Mostly correct. You cannot use the Writer class directly since it is an abstract class. You can do this:


Here the variable is a Writer, but the underlying object is a FileWriter.

And there are other ways to write to a file besides Writers.


can any of you tell me that,which class to choose and what is that "thing" ( or measure ) which tell me to choose a particular class.

There is no magic answer. The answer is "it depends on what you are trying to do". And what functionality you need or want. Do you need buffering? (Usually a good idea and helpful). Are you writing to a File or to a Stream?

And unfortunately, there really is no quick way to explain the Java I/O API. Nor is there a quick way to learn it. But it is something you really want to do. I know some developers with 5+ years experience that still just kind of guess when selecting an I/O class to use. Or they simply always use the same one. They never took the time to learn the IO API. I can recommend the book Java I/O, 2nd Edition as a good learning tool.

Also learning about and understanding the decorator design pattern can be very helpful in understanding the Java I/O API since it uses it extensively. There is a Wikipedia article on the decorator pattern that gives an overview of what it is. But at some point, you probably want to read a good Design Patterns book such as HEad First Design Patterns.

Now with that all said a basic way you can write to a file that will work in a majority of the cases is this:


This provides a FileWriter wrapped in a BufferedWriter. The BufferedWriter helps improve performance. The short comings of that is that it will encode your file in the system's default encoding. That's probably okay for basic stuff or while you are learning. But at some point, you will need to specify the encoding. To so such, you'll need to do something different, such as this:



If you are dealing with lines of data or want easy formatting, a PrintWriter and its various print() and println() methods can be useful.

I hope that helps. Again, there is no magic answer. It all depends on what you need to accomplish.
[ December 10, 2008: Message edited by: Mark Vedder ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Mark Vedder, thankyou for answering my question,
Hope i will get over this confusing I/O classes as i start using those.
so i think, at this point in time,its enough for me to know how to use these class rather than to know how to choose the best classes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
In more recent versions of Java, you can try the Formatter class, for plain text files. The one big advantage Writers have over Formatter is that it is easy to create a Writer to append text, but I have never found a way to persuade Formatter to append to a File.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
The short comings of that is that it will encode your file in the system's default encoding. That's probably okay for basic stuff or while you are learning. But at some point, you will need to specify the encoding. To so such, you'll need to do something different, such as this:


What is encoding mark?
I've come across this term many times but didn't still got its full meaning yet.
Is it like encryption.
Then how can we have our own encoding technique.
What is the advantage of having an own encoding over system encoding.


Throw some light on what we do in the above code on encoding?
Regards.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by ramya narayanan:


What is encoding mark?
I've come across this term many times but didn't still got its full meaning yet.
Is it like encryption.


Encoding, short for character encoding, refers to a standard way of representing characters in a file (and to a computer). Ultimately, computers only understand numbers. An encoding maps characters to numbers. For example, in ASCII -- a common and early encoding -- the uppercase letter 'A' is mapped to the decimal value 65.

Originally posted by ramya narayanan:

Then how can we have our own encoding technique.
What is the advantage of having an own encoding over system encoding.


You would not have your own. They are defined by standards organizations such as the International Organization for Standardization (ISO).

Originally posted by ramya narayanan:


Throw some light on what we do in the above code on encoding?
Regards.


The full code would be something like this:



or



To tell the system what encoding I want to use.

For more information:
  • Wikipdeia Character Encoding Article
  • Encoding Article at the Java Glossary
  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky at Joel on Software
  • Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    off to the intermediate forum


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19696
        
      20

    Originally posted by Bert Bates:
    off to the intermediate forum

    And now it's under my control, I'm moving it to I/O


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: File Writers