Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Writing to a File using PrintWriter and FileWriter?

daimj69daimj69 Posts: 27Member
Hi all.

Having a bit of trouble writing to a File. If I populate the .txt File and use a FileReader with a BufferedReader like this:

[code]BufferedReader in = new BufferedReader(new FileReader("password.txt"));

String input = in.readLine();[/code]

I can read the contents of the File. But when it comes to writing to the .txt File using a PrintWriter and a FileWriter like this:

[code]PrintWriter out = new PrintWriter(new FileWriter("password"));

out.println("123456,pass");[/code]

The .txt File just becomes empty, even if the File doesn't exist or is empty to begin with, all I get is an empty File. I'm a bit confused, and was wondering if anyone could shine some light on the matter, and tell me how to go about writing to a .txt File successfully. I've even tried a RandomAccessFile to write to a file, without any luck. So any help on writing to a File using a PrintWriter and a FileWriter would be appreciated.

Thanks in advance,
David.

Comments

  • fzammettifzammetti Posts: 68Member
    Try this instead:

    [code]
    File myFile = new File("password");
    Writer output = new BufferedWriter(new FileWriter(myFile));
    output.write("123456,pass");
    output.close();
    [/code]

    Generally speaking, it is better to use a generic interface reference rather than a specific class (for instance, you have the ability to switch from a buffered write method to an unbuffered one if you use Writer rather than BufferedWriter directly).

    I'm not 100% sure why your could didn't work, I'd have to look at some docs to figure it out. Nothing strikes me as obviously incorrect. But, the above I think is generally better anyway.

    : Hi all.:
    : [code]BufferedReader in = new BufferedReader(new FileReader("password.txt"));
    :
    : Having a bit of trouble writing to a File. If I populate the .txt File and use a FileReader with a BufferedReader like this:

    :
    : String input = in.readLine();[/code]
    :
    : I can read the contents of the File. But when it comes to writing to the .txt File using a PrintWriter and a FileWriter like this:
    :
    : [code]PrintWriter out = new PrintWriter(new FileWriter("password"));
    :
    : out.println("123456,pass");[/code]
    :
    : The .txt File just becomes empty, even if the File doesn't exist or is empty to begin with, all I get is an empty File. I'm a bit confused, and was wondering if anyone could shine some light on the matter, and tell me how to go about writing to a .txt File successfully. I've even tried a RandomAccessFile to write to a file, without any luck. So any help on writing to a File using a PrintWriter and a FileWriter would be appreciated.
    :
    : Thanks in advance,
    : David.
    :

  • daimj69daimj69 Posts: 27Member
    Thanks once again, the code you provided worked.

    But was wondering if I could ask a favour?

    Using the method you provided, the Data contained in the File is over-written with any new Data written to the File, is there anyway to add Data to the file i.e Append, so I can keep adding Account Records. Also what's the best way to search this File to find an Account Number? I had in mind reading in a Line at a time, then splitting it, and checking each part individually.

    There will be a second File which will hold further details for the Accounts, such as Balance. What's the best way (if there is one), to overwrite a specific line with new data, like when I want to update the Balance? Obviously I will have to search for the Line that belongs to a chosen Account Number. Can this be done using these types of files, or will RandomAccessFiles be required?

    Thanks in advance,
    David.
  • fzammettifzammetti Posts: 68Member
    Well, let me answer the second part first... As soon as you get into random access situations, I would suggest not using text files in the first place. One of the really nice things about Java is how trivially easy doing database programming is. I would suggest going that route. Choose the database you like (Access, SQL, Oracle, MySQL, etc.) and code for that.

    I have to admit I've never done random access text file work for exactly the reason stated above. I've actually not done a whole lot with text files anyway, except for XML and properties, which are of course handled in special ways. So, I don't actually know if it's possible to overwrite just part of a file.

    The question about appending instead of overwriting should be as easy as replacing new FileWriter(myFile) with new FileWriter(myFile, true). Take a look at the javadocs for the FileWriter class for further details.

    Lastly, as far as searching goes... As I said, I'm not that familiar with random access for text files, so there may be something of help in those classes that I'm ont aware of, but I'm not entirely sure that's true. So, assuming it isn't, I think you would have to read in the entire file and scan it (or at least scan it as you read in each line, but there's still the possibility that what your looking for is the last line you read, so I think it would ultimately be better to just read it all in one go, close it and scan it in memory). If you read in each line into an array of strings (or into an ArrayList better yet), you can then interate over each element and search them easily since the String class has some methods to do searches.

    But, going back to what I said in the beginning... it should be clear that this is horribly inefficient even in the best case. If you need to access data, and especially if you need to update data, in a random fashion, your really talking about something that should be done in a database. If you decide to take that on, feel free to post back here and I'll help get you started. The code really is quite simple (in a lot of ways even simpler than file access I think!)

    : Thanks once again, the code you provided worked.
    :
    : But was wondering if I could ask a favour?
    :
    : Using the method you provided, the Data contained in the File is over-written with any new Data written to the File, is there anyway to add Data to the file i.e Append, so I can keep adding Account Records. Also what's the best way to search this File to find an Account Number? I had in mind reading in a Line at a time, then splitting it, and checking each part individually.
    :
    : There will be a second File which will hold further details for the Accounts, such as Balance. What's the best way (if there is one), to overwrite a specific line with new data, like when I want to update the Balance? Obviously I will have to search for the Line that belongs to a chosen Account Number. Can this be done using these types of files, or will RandomAccessFiles be required?
    :
    : Thanks in advance,
    : David.
    :

  • daimj69daimj69 Posts: 27Member
    Thanks for the help, the Append works great.

    In the assignment specification, it mentions using files, so gonna try and do it this way, even though I can see that it isn't the best way to do things.

    Having said that though, once I get it all working and complete, maybe i'll try and use a different method, at least that way i'll have a working solution to hand in if the attempt at a more efficient way fails.

    If I can tickle your brain once more, what's the best way to read an entire file into memory, and then write it back to a file, any chance you can give us an idea code wise, that I can build on.

    Thanks in advance, you've been a great help.
    David.
  • fzammettifzammetti Posts: 68Member
    Well, there's probably about 100 different ways to do it... I think my inclination would be to read it one line at a time as you had been thinking of doing, store each line as a String object in an ArrayList, then deal with it from there. Makes it nice and easy to interate over all the lines, you don't have to worry about an array that is too smal, things like that.

    : Thanks for the help, the Append works great.
    :
    : In the assignment specification, it mentions using files, so gonna try and do it this way, even though I can see that it isn't the best way to do things.
    :
    : Having said that though, once I get it all working and complete, maybe i'll try and use a different method, at least that way i'll have a working solution to hand in if the attempt at a more efficient way fails.
    :
    : If I can tickle your brain once more, what's the best way to read an entire file into memory, and then write it back to a file, any chance you can give us an idea code wise, that I can build on.
    :
    : Thanks in advance, you've been a great help.
    : David.
    :

  • JawakaJawaka Posts: 1Member
    did u remember to close the writer in your first code?
    out.close();

    had the same problem once and it was just that;P

  • janekobwarzanejanekobwarzane Posts: 1Member
    Hi

    Try page:

    JAVA4.NET

    There are all possibilities of read/write files in Java.

    Regards
  • wisedwised Posts: 1Member
    Hay man! You saved my day! Closing the writer is a major issue ^^ Thanx again
Sign In or Register to comment.