Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > open FileWriter in main function and pass in as parameter

Reply
Thread Tools

open FileWriter in main function and pass in as parameter

 
 
moongeegee
Guest
Posts: n/a
 
      09-11-2007
I open a file by using FileWriter in main() function, the code as
below. I want to pass the outputStream
as parameter as foo(str,outputStream). Please help.

try {
FileWriter outputStream = new FileWriter("out.txt",true);
....
}
catch(...) {
}
foo(str,outputStream);

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      09-11-2007
moongeegee wrote:
> I open a file by using FileWriter in main() function, the code as
> below. I want to pass the outputStream
> as parameter as foo(str,outputStream). Please help.
>
> try {
> FileWriter outputStream = new FileWriter("out.txt",true);
> ....
> }
> catch(...) {
> }
> foo(str,outputStream);
>


FileOutputStream fos = null;
try {
fos = new FileOutputStream("file.name");
foo(str,fos);
} catch (IOException ioe) {
ioe.printStackTrace();
}

You can't mix writers and streams. Pick one or the other. The example
above uses streams. With the scheme above, if your file doesn't exist
or for whatever reason it throws an IOException your foo method won't
get called and blow up that code.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-11-2007
On Tue, 11 Sep 2007 02:19:45 -0000, moongeegee <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>try {
> FileWriter outputStream = new FileWriter("out.txt",true);


for sample code for all your java.io needs, see
http://mindprod.com/applet/fileio.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      09-11-2007
moongeegee wrote:

> try {
> FileWriter outputStream = new FileWriter("out.txt",true);
> ....
> }
> catch(...) {
> }
> foo(str,outputStream);
>


IIUC, and your problem is a scope of the local, try the following:

FileWriter outputStream;
try {
outputStream = new FileWriter("out.txt",true);
....


piotr
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      09-11-2007
Piotr Kobzda wrote:
> moongeegee wrote:
>
>> try {
>> FileWriter outputStream = new FileWriter("out.txt",true);
>> ....
>> }
>> catch(...) {
>> }
>> foo(str,outputStream);
>>

>
> IIUC, and your problem is a scope of the local, try the following:
>
> FileWriter outputStream;
> try {
> outputStream = new FileWriter("out.txt",true);
> ....
>
>
> piotr


Depending on what is in the catch block, outputStream may not be
definitely assigned at the end of the try-catch. If there are definite
assignment problems, initialize it in its declaration.

FileWriter outputStream = null;

Of course, the code should be written so that the null initialization
will not reach the use after the try-catch, for example because of a
System.exit call.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
without declare parameter [double square(parameter)] return 0 in main WanHongbin@gmail.com C Programming 5 10-01-2008 03:31 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
how #define a main() function and call our own main function? ravi C Programming 28 09-26-2007 01:33 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments