Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Testing File to see if it Exists, and writing meta data

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attached below is my main method where I am running into a few snags.
1st I am attempting to use the createFile method to check to see if a photo is found. If the photo is found (indicated by the file name the user enters) it is then supposed to return the photo data with the toString method from the photo class I have written. The problem with this is I do not know where this fits logically into the sequence of events, I am aware it should be using an if statement or similar loop but I need help with that part

The second problem I am running into is with saving the data entered to album.dat. I have got a rough setup and the problem I am running into is if a user enters multiple photos it simply writes over the first and only saves one photo's meta data. If someone would be so kind as to point me in the right direction I would greatly appreciate it thank you.

 
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I fully understand the problem.

As for writing data to a file, you are better off doing all the writing, with a buffered method, opening one writer (decorated with the file, etc) and closing it in a finally block at the end. Not writing one photo and then creating a new writer.
Many writers have a flag which can be set in the constructor so as to append the text rather than overwriting. You will have to go through the API documentation and look for such features.

I think one of your problems is that you are trying to do too much all at once. You should be reading the photos from a file, getting the Photo objects set up and displaying them as a test of the method before you go anywhere near writing. Then you would know what is going on. You would then not have written a method called readPhoto which claims to return the Photo, but also in the background, tries to write the data to a file. Think object-oriented. Get all the photos read, get them stored somewhere (most easily in a suitable list), then you can consider writing them. Remember the Photo object encapsulates all the data you require, so you can retrieve everything later.
 
Justin Core
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you, for the first part of the writing. I have created a new method and so far this is what I have


What I am a little confused on is how I should use the finally block and how and where exactly to call this in my main method. Thank you
 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth did you delete the first lot of code? That can make the whole thread incomprehensible.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!