File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Writers

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 8898
    5
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    off to the intermediate forum
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20396
    47
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Bert Bates:
    off to the intermediate forum

    And now it's under my control, I'm moving it to I/O
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic