Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Is there a "best" way to read/write files in Java?

  1. #1
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Is there a "best" way to read/write files in Java?

    I have seen different methods of creating and reading files (specifically text files) in Java. The PrintWriter method or the Formatter with a Scanner to read the file, using a BufferedWriter with a BufferedReader, etc. They will all read/write text files, but from what I understand they do so in different ways. That's about all I understand, though. When would it be more beneficial to use a buffered writer than, say, PrintWriter, which is much simpler code-wise? Is there a "best" way to handle i/o in general in Java?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Is there a "best" way to read/write files in Java?

    Depends on what you are doing.
    Q: PrintWriter or BufferedWritter?
    A: Both. You can take advantage of PrintWriter's simplified class while also taking advantage of buffering:
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
    It is important to note that PrintWriter "eats" exceptions, meaning: I/O exceptions will not be thrown down the stack. This can be problematic, because things may just not work, and you won't know why. It is generally suggested to use the PrintWriter.checkError() method to determine if the writer "ate" any exceptions.

    Q: Scanner or BufferedReader?
    A: Depends what you are doing. The BufferedReader will work in almost all cases, while the Scanner will work in some cases. If I can get away with using the Scanner to simplify things, I will. But, I usually find myself having to use the BufferedReader, because Scanner is not practical for my purposes. Generally speaking, you will not (*should not*) see large-scale commercial products reading files with Scanner, due to its limitations. However, when it comes to reading user console input or simple files in small programs, a BufferedReader may just be overkill for your purposes, so Scanner is a "better" option.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (April 1st, 2014)

  4. #3
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Is there a "best" way to read/write files in Java?

    Perfect, that's exactly what I wanted to know... PrintWriter.checkError() is a good tip, though I'll probably be sticking with just using a BufferedWriter every time so I can get into good habits. Thanks for the information!

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 0
    Last Post: October 5th, 2012, 07:27 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. access denied (java.io.FilePermission "report.jrxml" read)
    By banny7 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 3rd, 2011, 06:02 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM

Tags for this Thread