bob
bob

Reputation: 5

Where is the input is going?

I'm a bit new to programming and haven't used the buffered writer much at all.

As far as I can see my code is good. However I have no idea where, or even if, this is printing to a file. I can't find where the file is.

if (choice ==2) {
    String Id;
    String Name;
    String Time;
    int penalty;
    String Code;

    new FileWriter("res/dogs.txt",true);

    try {
        System.out.println("add a dog");
        System.out.println("enter dog ID (cancel to quit)");
        Id =k.next ();
        System.out.println("enter dog name");
        Name=k.next();
        System.out.println("enter dogs time");
        Time = k.next();
        System.out.println("enter penalty quantity");
        penalty = k.nextInt();
        System.out.println ("enter course code");
        Code = k.next();

        if (!Id .equalsIgnoreCase ("cancel")) {
            //true = append file
            FileWriter fileWritter = new FileWriter(Dog.getName(),true);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(Id);
            bufferWritter.write(" ");
            bufferWritter.write(Name);
            bufferWritter.write(" ");
            bufferWritter.write(Time);
            bufferWritter.write(" ");
            bufferWritter.write(penalty);
            bufferWritter.write(" ");
            bufferWritter.write(Code);
            bufferWritter.write(" ");
            bufferWritter.close();
        }
    } catch(IOException e){
        e.printStackTrace();
    }
} else if (choice == 3) {
    mainmenu();
}

Any ideas as to what I am doing wrong?

Upvotes: 0

Views: 86

Answers (2)

JNYRanger
JNYRanger

Reputation: 7097

There are a two errors regarding where you are writing to. The first is that you're creating a new FileWriter with your "res/dogs.txt" path at the beginning of your code in the try block, but you don't ever use it!

Later in your code, but before you start writing you call:

FileWriter fileWritter = new FileWriter(Dog.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

You then write to the BufferedWriter that is linked to the FileWriter with a filepath as Dog.getName() whatever that might be, and ignore the original filewriter you created.

Additionally, you should call your close() methods in a finally block like this:

FileWriter writer //or other resource using method
try
{
      //do stuff
}
catch(Exception)
{
    //handle problem
}
finally
{
      writer.close();
}

or even better with a Try with Resources like this:

 try(BufferedWriter writer = new BufferedWriter(otherWriter))
 {
      //do stuff
 }
 catch(Exception)
 {
       //handle error
  }

Upvotes: 1

Baby
Baby

Reputation: 5092

By default, file created using FileWriter will be saved in the directory where you run your program.

Note that you can specify where you want to save the file you created by adding folder path in your file name. for example, if you want to save the file in D:\ you can do this:

FileWriter fileWritter = new FileWriter("D:\\res\\"+Dog.getName()+".txt",true);

Upvotes: 1

Related Questions