Java - Writing to File Help Needed!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deeadeed
    New Member
    • Dec 2007
    • 1

    #1

    Java - Writing to File Help Needed!

    Hey,
    Im working on a programme and need it to write to file. I put in a file writer it seems to run ok, creates the txt file but doesnt write any data. Heres the code:

    private void requestJButtonA ctionPerformed (ActionEvent event)
    {

    Writer fw = null;

    try
    {
    //fw = new FileWriter( "customer.t xt" );
    final FileWriter outputFile = new FileWriter("cus tomer.txt");
    final BufferedWriter outputBuffer = new BufferedWriter( outputFile);
    final PrintWriter printstream = new PrintWriter(out putBuffer);
    fw.write( phoneJTextField + "," + postcodeJTextFi eld );
    }
    catch ( IOException e ) {
    System.err.prin tln( "Cannot write to file" );
    }

    finally {
    if ( fw != null )
    try { fw.close(); } catch ( IOException e ) { }
    }


    }

    any ideas then please help!

    Thanks

    Nicole
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by deeadeed
    Hey,
    Im working on a programme and need it to write to file. I put in a file writer it seems to run ok, creates the txt file but doesnt write any data. Heres the code:

    private void requestJButtonA ctionPerformed (ActionEvent event)
    {

    Writer fw = null;

    try
    {
    //fw = new FileWriter( "customer.t xt" );
    final FileWriter outputFile = new FileWriter("cus tomer.txt");
    final BufferedWriter outputBuffer = new BufferedWriter( outputFile);
    final PrintWriter printstream = new PrintWriter(out putBuffer);
    fw.write( phoneJTextField + "," + postcodeJTextFi eld );
    }
    catch ( IOException e ) {
    System.err.prin tln( "Cannot write to file" );
    }

    finally {
    if ( fw != null )
    try { fw.close(); } catch ( IOException e ) { }
    }


    }

    any ideas then please help!

    Thanks

    Nicole
    Welcome to TSDN!
    But fw is stil null.

    Debasis Jana

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by deeadeed
      Hey,
      Im working on a programme and need it to write to file. I put in a file writer it seems to run ok, creates the txt file but doesnt write any data. Heres the code:

      private void requestJButtonA ctionPerformed (ActionEvent event)
      {

      Writer fw = null;

      try
      {
      //fw = new FileWriter( "customer.t xt" );
      final FileWriter outputFile = new FileWriter("cus tomer.txt");
      final BufferedWriter outputBuffer = new BufferedWriter( outputFile);
      final PrintWriter printstream = new PrintWriter(out putBuffer);
      fw.write( phoneJTextField + "," + postcodeJTextFi eld );
      }
      catch ( IOException e ) {
      System.err.prin tln( "Cannot write to file" );
      }

      finally {
      if ( fw != null )
      try { fw.close(); } catch ( IOException e ) { }
      }


      }

      any ideas then please help!

      Thanks

      Nicole

      How to read and write text files

      Comment

      Working...