File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Murach's Java Servlets and JSP this week in the Servlets 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

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
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?

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
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 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.


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