Results 1 to 3 of 3
  1. #1
    superman5 is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default FileWriter doesn't write

    Hi

    I have this weird problem.

    I'm using NetBeans and the code below does not work. Nothing gets written to my output file.

    Java Code:
    import java.io.File;
    import java.io.FileWriter;
    
    public class NewClass {
    
        public static void main ( String args[]){
    
            try {
                File file = new File("myFile.txt");
                FileWriter fileWriter = new FileWriter( file );
    
                fileWriter.write( " test " );
            }
            catch ( Exception e ){
                e.printStackTrace();
            }
        }
    }
    This is part of a large application I'm writing in NetBeans and really need this to work.
    Any ideas?

    Thanks
    Last edited by superman5; 07-29-2009 at 08:20 PM. Reason: security

  2. #2
    superman5 is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Actually I just checked and NetBeans is not the problem.
    That code doesn't work anywhere.

    Can someone tell me what the proper way to write to a text file is?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Once you write something to FileWriter stream, until you flush them file is not updated.

    Java Code:
    fileWriter.write( " test " );
                fileWriter.flush();
    Read the JavaDoc for more details on flush().

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 997
    Last Post: 11-04-2014, 06:46 PM
  2. JAva Filewriter
    By tommyyyy in forum New To Java
    Replies: 1
    Last Post: 03-28-2009, 01:51 PM
  3. scanner vs. BufferedReader? printstream vs. FileWriter?
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 01-09-2009, 02:58 AM
  4. How to write....**
    By krichait in forum New To Java
    Replies: 9
    Last Post: 08-07-2008, 03:09 PM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 10:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •