This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Solution for  Overwriting Existing File Confirmations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Solution for  Overwriting Existing File Confirmations" Watch "Solution for  Overwriting Existing File Confirmations" New topic
Author

Solution for Overwriting Existing File Confirmations

Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Wasn't sure what to put in the subject line...

Essentially, I have my program working so that, when a user opens a new file, it'll simply say, "Oh hey, there is a file open, perhaps he should save it." As of right now, as long as the user has set about editing the file (literally clicked a button saying "I am going to edit the file" and making the fields editable), then they are prompted with a dialog asking Yes/No/Cancel to "Would you like to save?" jargon.

I have nothing for saving, because I don't know how I could do anything other than if the paths match, are you sure want to overwrite...

Simply put, is there a built in class for comparing files? Text files, specifically. What is the best way to go about this? I know I could code this myself, but there has GOT to be an easier way then the way I (a newbie) am thinking... I feel like Java is made for stuff like this.
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
HI,
i am not able to completly understand your problem. could you explain bit more!

To compare two text files one option is to check the length of the file if it is exceeding earlier value..or when you ask user while saving " do you want to overwrite " at that time if "yes" then write it and if "no" then append the data. Is this you want to say?

thanks,
Isha
Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Not quite...

I know how to prompt the user for a dialog with options. That part isn't hard.

I didn't know you could compare length of a file, but I knew you could compare size... however, if someone fixes a single character, the size of the file should remain the same while the character itself would be changed (I think).


So how can I compare the currently edited file to an older file, check to see if there are changes (only if the names match). If there are, prompt for save.

Maybe a better way is to have an "documentIsSaved" boolean or something, and the moment a user starts typing in any box, it'll change the title to add an asterisk and such like most programs do?

Then, if a user goes to saver over any old file, ask them if they want to save?

If they have no changes, and the file they are saving over is the same, just save (because it doesn't make a difference).


Keep in mind this is for use with both a 'Save' and 'Save as...' button.


Does my solution make sense, or is there something better I could do then adding a boolean like that and changing it when any field is typed into after the last save?
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
hi,
I have analyzed the situation and your solution is fine. My observation is
1. When user open a file,he/she would be entering data through some interface. Whatever data is entered is new entry to the file, even if its same..how can you comapre with whole file.
2. Add keyboard event to check if any changes has been done on doc even if data remains same. i.e your boolean check could be on Keyboard Event in in each field.


Thanks


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Solution for Overwriting Existing File Confirmations
 
Similar Threads
Saving a File
data saving question
Problem running jar
is PasswordAuthentication secure?
Creating an Update App