• 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

Is it possible to overwrite existing file data to avoid checking if data already exists?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

I am a Java noob.

Is it possible to open a file and add new data to that file where some of this data may already exist?

I was thinking I could overwrite existing data so that I would not have to keep checking if certain data already exists each time before I add new data to the file.

For example, consider the following file that contains the following data:

Bob isA Person
Bob hasAge 25

Later, I may receive the following information that I'd like to append to the existing file:

Bob hasAge 25
Bob hasHairColour Brown
25 isA Integer

I dont want to have to verify that each statement has not already been added previously.

Is it possible to add "Bob hasAge 25" again but not as a duplicate rather just add and overwrite if it all ready exists?

Note, I don't want to remove existing content of the original file. That is, based on the original file and the new data, I'd like to end with the following new/updated file:

Bob isA Person
Bob hasAge 25
Bob hasHairColour Brown
25 isA Integer

(note the above example is an overly simplistic example but is useful to demonstrate my overall objective where I will be manipulating text-based documents).

Thanks in advance,
Paddy.
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you dont need to overwrite file, you can open file in append mode. Also you can keep structured documents(prefered in xm format), to avoid duplication. xml structure will give you a particular place to add that description whhich you can override at any time you get updated info.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no way to accomplish that simply by writing to the file. You'll have to read the file in, break it up into individual pieces of information according to whatever your semantics are, and then write back out to the file.

If you're planning to add data to a section in the middle of the file, then you'll have to read and rewrite the entire file. (Actually, you can get away with just reading and rewriting everything starting with where the first new data will go, but that gets a bit more complex.)
 
Paddy Joe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses.

I am particularly interested in what harshvardhan said about the append option and XML.

In actual fact, I am working with OWL-DL (W3C ontology language) that is effectively an XML file.

I will be adding new xml excerpts (effectively new semantic/ontology knowledge encoded as XML) that I may receive.

However, I may already have received the same or similar XML excerpts from another source.

Example:
Alice may notify me of the following data to add to my file:
Bob

Later Eve may notify me of the following:
Bob hasAge 25

[In this case "Bob" already exists. I'd like a simple way to ignore/replace "Bob" on the fly so the file remains semantically the same and has no duplicates.]

Even later, Mary may send me the following data to be added the my file:

hasAge 25

[Again "hasAge 25 " already exists. So I was hoping there was some magic API call that handles this automatically without me having to handcraft code to specifically do the checks.]


Rather than having to try and parse out all the XML (I am not an XML expert) each time I want to add something (and having to make sure if it exists leave it alone, if not add it), I was thinking alone the lines of an automatic replace option. Consider the following analog: if I copy 5 photos to a folder whereby 2 of those photos already exist in that folder, the system will either ignore/replace those photos (on the fly).

I am not sure I fully understand harshvardhan's comment. Are you saying there is some special Java API to manipulate XML-based files that will automatically *on the fly* ignore/replace duplicate parts when adding new XML excerpts. If so this is exactly what I want.

 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic