Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: File Writer not writing no error in code

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default File Writer not writing no error in code

    I checked the path multiple times, the file where it reads from is in the same location
    ("C:\\Users\\Toshiba\\Desktop\\read.txt");
    but can't write in a new text file

    public static void sauve(String a , String b , int c , String d , int f ) {
    int i;
    try {
    FileWriter fw=new FileWriter("C:\\Users\\Toshiba\\Desktop\\result.tx t");
    BufferedWriter bw= new BufferedWriter(fw);
    bw.write("Name : " +d);
    bw.newLine();
    bw.write("question number : "+ f);
    bw.newLine();
    bw.write("start time "+ a);
    bw.newLine();
    bw.write("Score : "+ c);
    bw.newLine();
    bw.write("finish time "+ b);
    bw.close();
    } catch (Exception e) {
    System.out.println("Error "+e);
    }
    }
    Last edited by e93; January 5th, 2014 at 04:48 AM. Reason: space in txt


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: File Writer not writing no error in code

    Why is there a space in "tx t"?

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: File Writer not writing no error in code

    My bad there isn't a space
    The code is not working. not creating the result file

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: File Writer not writing no error in code

    Please post your code in code or highlight tags.

    The space in "tx t" was the only nit I could see, and without that your method works fine for me. The file is created and it contains the data I sent to it. Either you're not looking for the file in the correct location on your computer, or it's there and you're not seeing it.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: File Writer not writing no error in code

    I'm editing the code without the space it's not saving it, i'm saving

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: File Writer not writing no error in code

    Here's the code that works fine for me. The only line I changed is the file name:
    import java.io.FileWriter;
    import java.io.BufferedWriter;
     
    public class TestClass
    {
        public static void main( String[] args )
        {
            sauve( "1735", "1927", 117, "My Name", 32 );
        }
     
        public static void sauve( String a , String b , int c , String d , int f )
        {
            int i;
     
            try
            {
                FileWriter fw = new FileWriter( "./files/result.txt" );
                BufferedWriter bw = new BufferedWriter( fw );
                bw.write( "Name : " + d );
                bw.newLine();
                bw.write( "question number : " + f );
                bw.newLine();
                bw.write( "start time " + a );
                bw.newLine();
                bw.write( "Score : " + c );
                bw.newLine();
                bw.write( "finish time " + b );
                bw.close();
            }
            catch ( Exception e )
            {
                System.out.println( "Error " + e );
            }
        }
     
    }

Similar Threads

  1. Apprending a print writer file
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 08:45 AM
  2. Writing code to save in a file... help please
    By rchrist3 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 13th, 2013, 11:51 AM
  3. Read Writer button wont add to file.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 08:08 AM
  4. Help writing to a file getting an exception error
    By loui345 in forum What's Wrong With My Code?
    Replies: 34
    Last Post: October 2nd, 2012, 06:45 PM
  5. [SOLVED] Buffered writer in a .jar file
    By lorider93p in forum Java Theory & Questions
    Replies: 8
    Last Post: February 8th, 2012, 01:03 PM