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++)

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.

ps. if you go to 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?

C:\temp>java RandomSeq 10

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

C:\temp>more txt.txt

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?


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.
2. In the Interactions pane, import the classes from the package: import* 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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.