• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Different FileWrite constructors and methods for appending data

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Complete noob here, so forgive the elementary question. This relates to a Whizlabs question, but I'm looking for more high level information.

Assume I have a file "abc.txt" with existing data. I want to write to the file, keeping all prior data intact - in essence, add new data to the end of the existing file.

My thinking is that there are (at least) two ways to do this:

1) use the overloaded constructor ("abc.txt", true) in conjunction with the write() method to automatically append new data

or

2) use the constructor without the boolean argument ("abc.txt") in conjunction with the append() method

I don't see any reason these aren't the same thing, unless I'm missing something.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you talking about the FileWriter class ?
 
Ron Lipke
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, yeah I mistyped that in the subject line, haha.

FileWriter class it is.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both calls perform the same action functionally. I am not aware of a technical difference. Ideally there shouldn't be one.

When I get the time, I will try to confirm the difference if there is one
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:Both calls perform the same action functionally.



"append" and "write" are the same, you mean. Yes, they are, they're identical. "append(c)" exists just to satisfy the "Appendable" interface, and is implemented by calling this.write(c).

Your two examples are not the same. If you use the FileWriter constructor with just one argument, the file is immediately truncated to zero bytes; the original contents are lost. If you use the two-argument version and pass "true", however, then the file's contents are left intact.
 
Ron Lipke
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:[
Your two examples are not the same. If you use the FileWriter constructor with just one argument, the file is immediately truncated to zero bytes; the original contents are lost. If you use the two-argument version and pass "true", however, then the file's contents are left intact.



This is what I was looking for. I couldn't find where it says the original file contents are lost with the single argument constructor.

Thanks!
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your two examples are not the same. If you use the FileWriter constructor with just one argument, the file is immediately truncated to zero bytes; the original contents are lost. If you use the two-argument version and pass "true", however, then the file's contents are left intact.



I am aware that that is the case. Functionally the functions write and append do the same thing when the writer is configured in appending mode.

After re-reading the OP's question, it appears he wanted to know if using the ("abc.txt") constructor would erase his original data. I missed that. Your answer clarifies it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic