-2
public static void main(String args[]) {
    //print a random sequence of N real values in [0,1)
    int N = Integer.parseInt(args[0]);
    for (int i = 0; i < N; i++) {
        System.out.println(Math.random());
    }
}

The code is simple. Let's say N == 5. Now, I would like to generate 5 random numbers from [0,1) and store them into a file "data.txt". I do know how to do this in terminal: % java RandomSeq 5 > data.txt. It creates a file "data.txt" and stores the generated random numbers into it. However, how do I do this with Eclipse? I know how to pass in arguments in Eclipse but when I write, It doesn't work. I also tried to use the VW arguments but that did not work, as well.

luk2302
  • 55,258
  • 23
  • 97
  • 137
saness
  • 3
  • 1
  • Possible duplicate of [How do I create a file and write to it in Java?](https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java) – luk2302 Nov 28 '18 at 17:56
  • not exactly, I would like to learn with arguments. This way you can read the files and apply them to other programmes. – saness Nov 28 '18 at 18:03
  • what arguments? You already know how to pass in `N`, a file name is exactly the same thing. – luk2302 Nov 28 '18 at 18:05
  • I will wait for better responses. Thanks. – saness Nov 28 '18 at 18:37
  • "Redirecting standard output to a file in Eclipse" is a better title I guess. – saness Nov 28 '18 at 18:39
  • *I know how to pass in arguments in Eclipse but when I write, It doesn't work.* That's not a very useful problem description. We have no idea what you tried or what the result was. – shmosel Nov 28 '18 at 19:12
  • You're right. I will put more effort to describe the problem better, next time. – saness Nov 28 '18 at 19:26

1 Answers1

0

To redirect the console output in Eclipse open 'Run > Run Configurations....' and find your program in 'Java Application' section.

On the 'Common' tab of the configuration in the 'Standard Input and Output' section you can select 'Output File' and specify where the output goes.

greg-449
  • 109,219
  • 232
  • 102
  • 145