This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Writing to a file...Please help... 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 » Beginning Java
Bookmark "Writing to a file...Please help..." Watch "Writing to a file...Please help..." New topic
Author

Writing to a file...Please help...

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ok you guys...I tried to do something different...I can read files great like this...but now I am trying to write to a file...
Here's what I have so far...
BUT I'm no where near done...I still need to ask a user if he wants to read from a file or write to one..duhhhhhhhhhhhh I have no idea what I'm doing. Any suggestions???
I get an error with this code...here's the error..

F:\Java II\PetRecordDemo1.java:58: 'catch' without 'try'
catch (IOException e)
^
1 error

Tool completed with exit code 1

I know it's telling me I need a 'try'...but why?? Duhhhhhh, on my part.





[ March 23, 2005: Message edited by: Rose Evans ]

) ) ) )
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Try using just one catch block and in that block you can check for what kind of exception you've caught and proceed accordingly.

example psuedo code;

catch exception
if exception is IO do this
if exception is whatever do this
else do a generic error message
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Actually, the cause of the compiler error message, if this code snippet is accurate, is that you have omitted an ending brace in the for loop of your first catch block. So the compiler sees the second catch as occurring inside the first catch. See?

It's perfectly legitimate to list catch blocks in ascending order of the exception heirarchy. I've never seen multiple exceptions handled inside one catch block, and I know that had I done so in my last job my code never would have made it through the technical review board.
[ March 23, 2005: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Looks like a misplaced curly bracket.

The for loop within catch(FileNotFoundException e) block is missing a curly bracket and then after the catch (IOException e) block there is an extra curly bracket.


ack. I really botched it when I tried inserting a code block. Maybe you'll get the idea without it.
[ March 23, 2005: Message edited by: Carol Enderlin ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Carol -

Could you please reformat your code block? It's causing the topic to stretch way off to the right because there are no line breaks in your snippet.

[Nevermind! You beat me to it!]

Thanks!
[ March 23, 2005: Message edited by: Jeff Bosch ]
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
OK guys...I figured something out all by myself!!! Yeahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh!!!

Thanks for your help....I'm gonna see if I can make it do what else I want it to do!!!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Cool! I believe that if you figure it out yourself, the knowledge gained will always be yours.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
It's perfectly legitimate to list catch blocks in ascending order of the exception heirarchy. I've never seen multiple exceptions handled inside one catch block, and I know that had I done so in my last job my code never would have made it through the technical review board.


Whoops sorry about the bad advice. I've seen something like this before

And thought I'd throw it out, but I'm happy to learn something new and thank you for the correction.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I'm not sure that it's bad advice, it's just something I've never seen before. Sometimes questions like this come down to the standards used by whatever shop the programmer works in.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
OK line breaks in my snippets? Now how is it I go about making my stuff not so long where it doesn't run off the page? Hahhahahaaa...I concentrate so hard on my studies....till I am just clueless to anything....beginning to think I'm brain dead for sure.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Rose -

That was for Carol, not for you. Your code snippets are well-snippeted!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Jeff,

Oh ha ha...well I thought it might have been me who had the bad snippets..all of my code and stuff is running off the side of the page too, everyone's is..and I'm having to scroll around to read them. Ha.

Wonder why it's doing that?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Rose -

When you use the code tags, ultimatebb preserves the formatting. Part of that formatting is line-wrapping. If the line's not wrapped in your code, it won't wrap in the browser. I try to make sure my code doesn't go beyond column 60 to avoid the no-wrap problem. If my original code is wider than that, I'll find a way in my post to break the line into two. After all, Java, like C and C++, doesn't care a'tall about white space.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Jeff,

Oh I see, well you surely taught me something today. Give yourself a pat on the back.
I'll try to remember that next time, so my stuff won't wrap. Ha.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
No wrap music here, eh?

(Did I just hear a collective groan?)
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ha ha....you're funny!!!
My Java programming is doing great!!!
I figured out something all by myself. Yeah,
patting self on back!!!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Hentay -

Whoops sorry about the bad advice. I've seen something like this before




I spoke in haste. This type of exception handling is common when you have to determine the exception type from among multiple exceptions at the same level of the exception heirarchy. In such a case, you can catch the common superclass of the exception, then query the exception type.

Sorry for the confusion!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a file...Please help...
 
Similar Threads
Entering many records..Need help!
reading input from a file into a array
Input/Output PetRecord
Input files???
Illegal Start of Expression