Java program hangs on new PrintWriter

Discussion in 'Java' started by Oz Levanon, Feb 29, 2004.

  1. Oz Levanon

    Oz Levanon Guest

    Hi.
    I have a java program running on linux (Redhat 9, Java 1.4.2, build
    1.4.2-b28).
    The program periodically accesses a file on the hard disk using the
    following code:

    FileOutputStream fos = new FileOutputStream(filename);
    PrintWriter pw = new PrintWriter(fos);
    // ...
    // writes stuff to the file.
    // ...
    pw.close();
    fos.close();


    Every now and then (sometimes after hundreds of times, sometimes after
    less) the program hands. I can see from debug prints I've added that
    it is stuck after the new FileOutputStream(filename) and before the
    new PrintWriter(fos);
    The program doesn't exit (and no throwable is thrown) but rather stays
    up and running, but never gets past this line.
    Does anyone have any idea what could cause a program to hang in this
    line? Can anything on linux be done to a file that will cause a
    program trying to access it hang?

    TIA, oz.
     
    Oz Levanon, Feb 29, 2004
    #1
    1. Advertising

  2. Oz Levanon

    hiwa Guest

    (Oz Levanon) wrote in message news:<>...
    > Hi.
    > I have a java program running on linux (Redhat 9, Java 1.4.2, build
    > 1.4.2-b28).
    > The program periodically accesses a file on the hard disk using the
    > following code:
    >
    > FileOutputStream fos = new FileOutputStream(filename);
    > PrintWriter pw = new PrintWriter(fos);
    > // ...
    > // writes stuff to the file.
    > // ...
    > pw.close();
    > fos.close();
    >
    >
    > Every now and then (sometimes after hundreds of times, sometimes after
    > less) the program hands. I can see from debug prints I've added that
    > it is stuck after the new FileOutputStream(filename) and before the
    > new PrintWriter(fos);
    > The program doesn't exit (and no throwable is thrown) but rather stays
    > up and running, but never gets past this line.
    > Does anyone have any idea what could cause a program to hang in this
    > line? Can anything on linux be done to a file that will cause a
    > program trying to access it hang?
    >
    > TIA, oz.

    Try:

    Code:
    try{
    PrintWriter out = new PrintWriter(new BufferedWriter(new
    FileWriter("your-output-file")));
    //... do output
    out.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    
     
    hiwa, Mar 1, 2004
    #2
    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. Houman
    Replies:
    2
    Views:
    721
    Frank
    Aug 18, 2004
  2. Sharp
    Replies:
    5
    Views:
    409
    Paul Lutus
    Oct 15, 2004
  3. Lars Willich
    Replies:
    0
    Views:
    616
    Lars Willich
    Jan 28, 2005
  4. CamT
    Replies:
    1
    Views:
    4,440
    Oscar kind
    Mar 16, 2005
  5. yzzzzz

    Writer and PrintWriter

    yzzzzz, Mar 25, 2005, in forum: Java
    Replies:
    2
    Views:
    4,364
    yzzzzz
    Mar 25, 2005
Loading...

Share This Page