java.util.Formatter problem

import java.util.Formatter;

import java.io.PrintStream;

import java.io.File;

import java.io.FileNotFoundException;

class Turtle

{

private String name;

private Formatter f;

public Turtle(String name, Formatter f){

this.name = name;

this.f = f;

}

publicvoid move(int x ,int y){

f.format("%s The Turtle is at (%d,%d)\n", name, x, y);

}

publicstaticvoid main(String[] args)

{

PrintStream outAlias = System.out;

try

{

File file =new File("E:\\test\\temp.txt");

Turtle tommy =new Turtle("Tommy",new Formatter(file));

Turtle terry =new Turtle("Terry",new Formatter(file));

tommy.move(0,0);

terry.move(4,8);

tommy.move(3,4);

terry.move(2,5);

tommy.move(3,3);

terry.move(3,3);

}

catch (FileNotFoundException fnfe)

{

System.out.println("File not found.");

}

}

}

when i went throught the code for the formatter, it said that if a give a file name as an arguement to the constructor then the output is written to the file but i dont see any content in that file.

could anyone suggest whats wrong with this program

Thanks

[2418 byte] By [Austina] at [2007-11-26 21:51:38]
# 1

I think using the same file for creating two formatter might have an adverse effect on the behaviour.

But most importantly, you should probably flush/close your formatter (as it is buffered).

(Note that I'm still using 1.4, so these are only guesses based on the doc, which says If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.)

So what about the following:Formatter formatter = new Formatter(new File("E:\\test\\temp.txt"));

Turtle tommy = new Turtle("Tommy", formatter);

Turtle terry = new Turtle("Terry", formatter);

tommy.move(0,0);

terry.move(4,8);

tommy.move(3,4);

terry.move(2,5);

tommy.move(3,3);

terry.move(3,3);

formatter.flush();

formatter.close();

TimTheEnchantora at 2007-7-10 3:45:11 > top of Java-index,Java Essentials,Java Programming...