aspose file tools*
The moose likes Beginning Java and the fly likes How should file output be done? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How should file output be done?" Watch "How should file output be done?" New topic
Author

How should file output be done?

Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
OK friends, I spoke (& chugged) too soon. In my hasty exuberance, I have messed up my output. Here's the code
<code>
public void SaveFile() {
// Set up the file output stream
String outFile = "testfile.out";
try {
BufferedWriter fileOut =
new BufferedWriter(new FileWriter(outFile));
} catch (IOException e) {
System.out.println(e.getMessage());
}
String lineOfOutput;
// write the 50 element array to the disk
for (int Indx=0;Indx<50;Indx++) {
try {
lineOfOutput = Comment1[Indx];
( NOTE: there'll be add'l fields here later))
fileOut.write(lineOfOutput,0,lineOfOutput.length());
fileOut.newLine();
} catch (IOException e) {
System.out.println(e.getMessage());
}
} // for
fileOut.flush();
fileOut.close(); // close the file
}//SaveFile
</code>
The compiler produces this message
FileEdit.java:209: Undefined variable or class name: fileOut
fileOut.write(lineOfOutput,0,lineOfOutput.length());
^
FileEdit.java:211: Undefined variable or class name: fileOut
fileOut.newLine();
^
What started this whole mess was my total lack of understanding of the throws IOException clause. It seems I can't use it, but rather I must use the Try/Catch combo. I was receiving an error msg that told me I either had to catch an exception, or throw it in my method. I could get neither to work. Thanks again for the help AND the patience. -Bill.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Bill,
Any set of curly braces defines a "scope". Variables declared inside one scope aren't visible outside that scope. You added a "try" block around the declaration of fileOut, so now it's local to that block, meaning that the code later in the method can't see it.
Generally, there are two ways to deal with this. One is to declare the variable before the try block, and initialize it inside the try block:

This leads to fairly ugly code, so the second alternative is better: make the try block as big as possible, so all the code that need to access fileOut is inside the try block. Im the code you show, you have two separate small try blocks. Instead, have one big one that's basically the whole body of the method, and one catch block to handle IOExceptions.


[Jess in Action][AskingGoodQuestions]
Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
Thank you both so much for the "Scope lesson". SLAP! I should have seen that. SLAP SLAP! Now, am I better off using your suggestions, or using the "throws IOException" clause. If the latter, where do I put it?
Thanks again, -Bill
I've crossed that fine line between Genius, & Dufus...again!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Speaking in sweeping generalities:
The question to ask yourself is whether code that calls saveFile() wants to know whether the save operation failed or not. The answer is probably "Yes, it does." In that case, you remove the try/catch entirely for the moment, and then declare saveFile() as

Now, if saveFile() fails, it will throw an IOException to notify its caller. That means that to call saveFile(), you have to do it like this:

As you can see, there's no free lunch -- somebody's got to handle the exception. But generally, exceptions should end up being caught by the part of your code that knows how to deal with failure.
Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
Earnest, thanks again. I think it's finally starting to sink in. If I throw the exception, then the code that calls the routine has to either handle it (using the Try/Catch) or pass it on up the line (throw it)to the code that call IT, etc., etc. So, there "always a CATCH, somewhere" Correct? Sorry about the pun, it's just way too early. Thanks, -Bill.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Actually, that's kind of a cute joke and I can't remember ever hearing it before (incredibly enough!)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Just as an aside, Bill -- the UBB tags require square brackets, not angled ones...use [CODE] and [/CODE] instead of <code> and </code>.
Just a (hopefully) helpful note.
And I liked the pun, too...


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
Joel, thanks, I noticed that MY code didn't look quite the same as the other folks, especially in the "indentation area" HA!HA! So I went looking & found my <> vs [] problem. Thanks again. -Bill.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How should file output be done?