so i have a little issue here. i have a program that creates a list of random numbers, for sake of example here it is:

public class RandomSeq
{
  public static void main(String[] args)
  {
    int n = Integer.parseInt(args[0]);
    for (int i = 0; i < n; i++)
      System.out.println(Math.random());
  }
}

now my textbook says that all i have to do to get the output of this program to go into a text file is to type "java RandomSeq 10 > data.txt". this should make a text file (data.txt) that has a list of ten random doubles between 0 and 1 . but in reality what happens is it just sort of ignores the " > data.txt" part and just prints the ten random doubles in the console. the textbook also tells me that i can use a similar line of code to use a text file as imput (with standard input). for example if i have a program that uses the list of ten doubles as input i could type "java inputprogram < data.txt". but this also doesnt work.


so my question is to all you java gurus. is there something i need to do for this type of thing to work? i would assume that the textbook is not wrong in what it is saying to do, i wrather think there is something missing from either my compiler or some basic methods or something.

EDIT:
ps. if you go to http://www.cs.princeton.edu/introcs/15inout/ and look at some of the examples in the "redirecting and piping" and "standard drawing" sections as you scroll down, you can see what i mean. the " | sort" and " | more" commands would go under the same category i guess.. how can i get these to work?

Edited 6 Years Ago by pumpkin_pub: n/a

C:\temp>java RandomSeq 10
0.1995878045764118
0.18697513449515613
0.6346645242682385
0.6909903957451974
0.05055577107407527
0.8429962094911159
0.8997199708121247
0.753002832281699
0.7655982819397776
0.4395337156072403

C:\temp>java RandomSeq 10 > txt.txt

C:\temp>more txt.txt
0.45298364345396724
0.052193562285097195
0.5216091648361046
0.3600187351861466
0.6047989454746013
0.9062731448554414
0.8493113352078762
0.21295096211349018
0.8555603561942511
0.45169566534727856

This was in windows xp, I got the same result with linux

Copy/Paste exactly what you did and saw, what OS you're running.

Try something simple that's not related to java, at the console type

echo Hello > mytest.txt
more mytest.txt

does Hello appear in the text file?

wow

my own stupid laziness and "antsyness", if such a thing were to exist..

the trouble was that i was using drjava to try and create the file and read the file. i was not using the cmd prompt.. silly me ..

it worked perfectly once i switch to using the cmd prompt to run instead of stupid old drjava.

DrJava doesn't support the syntax java RandomSeq > mytest.txt You can achieve the same by doing this:

1. Compile your program (e.g. RandomSeq.java).
2. In the Interactions pane, import the classes from the java.io package: import java.io.* 3. Store the old System.out stream in a variable: PrintStream oldOut = System.out 4. Create a new PrintStream that writes to a file: PrintStream newOut = new PrintStream(new FileStream("mytest.txt")) 5. Set the new stream as System.out: System.setOut(newOut) 6. Now run your program (but not using the “Run” toolbar button or menu item): java RandomSeq 7. Your program will run, but all output from System.out is written to the mytest.txt file.
8. To get the original behavior of System.out back, set the old stream again: System.setOut(oldOut)

Edited 6 Years Ago by mgricken: n/a

This question has already been answered. Start a new discussion instead.