I cannot figure out why the following simple program doesn’t (create) and then write to a file? Can you spot where the problem is?
public class RandomSeq
{
public static void main( String[] args)
{
// command-line argument
int N = Integer.parseInt( args[0] );
// generate and print N numbers between 0 and 1
for ( int i = 0 ; i < N; i++ )
{
// System.out.println( Math.random() );
StdOut.println( Math.random() );
}
}
}
When I type the following at the Interactions prompt:
java RandomSeq 5
0.9959531649155268
0.5010055704125982
0.4444779637605908
0.4205901267129799
0.09968268057133955
I obviously get the correct output, but when I use piping, it doesn’t do what (I think) it should do:
> java RandomSeq 5 > f1.txt
The “normal” Java way to write to a file is by using a
Writerclass. I denoted a small example below. Alternatively you can change thePrintStreamthat you write to which then sends the output to a file instead of the console (essentially the same as below)Edit:
On my machine the code works fine using
System.out.println(). I can only imagine that there might be a problem with the write permissions, else wise maybeStdOutobject has some bugs…