simple file output question thanks in advance

ok so i wrote a program that reads from a file and outputs to a file. thing is the input file has several numbers and the output file is just printing the last answer(overwriting the others)
the program takes a number ex"683" finds the largest and smallest digit in the number then calculates its range
below is the part that prints to the output
public static void main(String args[]) throws FileNotFoundException
{
Scanner input = new Scanner (new File (args[0]));
while(input.hasNextInt())
{
int num = input.nextInt();
int range = digitRange(num);
PrintStream output = new PrintStream(new File("digit-range.out"));
output.print("The digit range of" + "\"" + num + "\"" + " is " + range);
}

Are you sure that you want to re-create the PrintStream object "output" inside the while loop? This will surely over-write anything that was written there before from the previous iterations of the loop. Why not create this object once, before the while loop, print to it inside the while loop, and close it after the while loop? Just a thought.

Yep: wrap your PrintStream around a FileOutputStream and make that latter stream append its stream contents (see the constructor for that class).

kind regards,

Jos

I don't think that this is going to help the OP and you may want to re-read the original question. I don't read it as his desiring to append to an existing file, but rather of a problem where he only writes the last bit of data to the file rather than all the data that was analyzed in previous iterations of the while loop.

I don't think that this is going to help the OP and you may want to re-read the original question. I don't read it as his desiring to append to an existing file, but rather of a problem where he only writes the last bit of data to the file rather than all the data that was analyzed in previous iterations of the while loop.