• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Writing modified content to a file

 
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a text file that has content which I read using a Scanner, line by line, adding each line to a List.

The content is modified by user interaction via a console application and saved.

A tutorial showed me that I need to do the following to save the file:

1. Write the content to a new file.
2. Delete the old file.
3. Rename the new file to the name of the old file.

My questions are:

1. When I open a file in Notepad and change some content inside an existing file, does the above procedure happen each time I press Save?
2. When I open an Excel spreadsheet, does the above procedure happen each time I press Save?
3. Is this the general way in which files are written by applications (Photoshop, Illustrator, etc.), which is invisible to the user?
 
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ask you to consider an ASCII text editor which opens an ASCII text file. Now you modify the contents of that file in the ASCII text editor. How do you update the persistent memory(we'll say hard drive) that contains the original? Can you put forth a scheme that'll work without creating a new file?
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible that some operating systems have a function which modifies an existing file by truncating its size to zero. If you used this, you could then write your data to that file without having to go through the write-delete-rename process.

However most professionally-written applications wouldn't do that because there's a risk of failure between "truncate" and "write", which would cause the data to be lost. Or if they did they would keep a backup copy somewhere and only delete it after all of the data was successfully written.
 
Paul Clapham
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also do this:

1. Delete the old file.
2. Write the content to a new file (with the same name).

But as you'll see, that has the same risk as the truncate-and-write method which I described in the previous post.
 
Jay Rex
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:I ask you to consider an ASCII text editor which opens an ASCII text file. Now you modify the contents of that file in the ASCII text editor. How do you update the persistent memory(we'll say hard drive) that contains the original? Can you put forth a scheme that'll work without creating a new file?



I can't, but that doesn't mean there isn't one. Hence my question

As a novice programmer, I find this hard to believe. I use Outlook all day at work. My current .PST file is 6GB, but I work on a PC where the .PST file is 20GB. Am I to believe that Outlook rewrites the 20GB file every time a new email arrives or every time I create a  task, or save a draft? Outlook would be completely unusable if that was the case.
 
Paul Clapham
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rex wrote:As a novice programmer, I find this hard to believe. I use Outlook all day at work. My current .PST file is 6GB, but I work on a PC where the .PST file is 20GB. Am I to believe that Outlook rewrites the 20GB file every time a new email arrives or every time I create a  task, or save a draft? Outlook would be completely unusable if that was the case.



You're making the assumption that Outlook starts by loading the entire .PST file into memory, for a start. At least that was the context of your original question. And yes, Outlook would be completely unusable if it did that. So I believe it doesn't do that.

But that's different from your original question, which asked about applications which read data into memory, allow the user to modify that data, and then save the modified data. Outlook clearly isn't one of those applications.

You could ask about a database, too. Maybe you have an SQL database where you work. And no, the database server doesn't rewrite the entire database every time a record is added. It also clearly isn't one of the applications you asked about.

By the way, did you ask your question out of curiosity or were you thinking "There's gotta be a better way than that"?
 
Jay Rex
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You're making the assumption that Outlook starts by loading the entire .PST file into memory, for a start. At least that was the context of your original question. And yes, Outlook would be completely unusable if it did that. So I believe it doesn't do that.



This thread has not offered any alternative way of thinking about how real applications load and save a single file, so that does lead to that assumption, so I challenged that with my real world example and your response was "So I believe it doesn't do that". Hence, write, delete, rename seems dubious as the way in which some applications work with a single file.

For my trivial programming task, write, delete, rename makes sense as the file is tiny, but that is not what I encounter as a user on a daily basis with real applications. My question comes from the perspective of trying to understand how real world applications deal with this problem. Hence, my three real world questions at the end of my initial post.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a basic text file, that is the way to do it.

A pst or similar are more akin to random access files, though.
They are a slab of disk space, some areas with useful data in, others free space for expanding into later.  PSTs have a whole structure to this slab, with indexes, folders, etc etc.
 
Paul Clapham
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rex wrote:My question comes from the perspective of trying to understand how real world applications deal with this problem. Hence, my three real world questions at the end of my initial post.



Unfortunately you're asking us to say that all real-world applications work the same way, and it's like this. And that's assuming that we even know the internal workings of those applications. Neither of those is true.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic