open FileWriter in main function and pass in as parameter

Discussion in 'Java' started by moongeegee, Sep 11, 2007.

  1. moongeegee

    moongeegee Guest

    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);
     
    moongeegee, Sep 11, 2007
    #1
    1. Advertising

  2. 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/
     
    Knute Johnson, Sep 11, 2007
    #2
    1. Advertising

  3. moongeegee

    Roedy Green Guest

    On Tue, 11 Sep 2007 02:19:45 -0000, moongeegee <>
    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
     
    Roedy Green, Sep 11, 2007
    #3
  4. moongeegee

    Piotr Kobzda Guest

    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
     
    Piotr Kobzda, Sep 11, 2007
    #4
  5. 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
     
    Patricia Shanahan, Sep 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mario Maestro
    Replies:
    5
    Views:
    25,683
    Michael Borgwardt
    Sep 17, 2004
  2. FileWriter and Path

    , Apr 4, 2007, in forum: Java
    Replies:
    1
    Views:
    705
    Andrew T.
    Apr 4, 2007
  3. ravi
    Replies:
    28
    Views:
    997
    Richard Heathfield
    Sep 26, 2007
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,690
    Roedy Green
    Sep 15, 2011
  5. AzamSharp
    Replies:
    2
    Views:
    187
Loading...

Share This Page