How to write random ints to a text file in Java

1 Answer

0 votes
// java code that write 30 random ints to a text file, 1 per line

package javaapplication1;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Random;

public class JavaApplication1 {
  
    public static void main(String[] args) {
        try
        {   
            try (PrintStream ps = new PrintStream(new File("D:\\data.txt"))) {
                Random rnd = new Random();
                final int COUNT = 30;
                
                for (int i = 0; i < COUNT; i++)
                    ps.println(rnd.nextInt());
            }
		}
		catch(IOException e)
		{	
            System.out.println(e);
		}
    }
}

//   data.txt
//  ----------
/*
    1137678701
    1367830938
    1359911308
    1830204569
    1168528758
    1826657123
    -38268684
    -1404012203
    1393657144
    -1197313728
    1962830723
    -463206344
    693134846
    -1117171264
    -1304279830
    -1508809020
    -1529927328
    1682971389
    -1055433677
    287010995
    441263796
    1804703998
    -413309858
    -1397907725
    -625987645
    -1819457955
    1270663724
    -1683440910
    -1687421761
    448625449
*/

/*
run:
   

*/

 



answered Jun 30, 2017 by avibootz

Related questions

1 answer 183 views
183 views asked Jun 30, 2017 by avibootz
1 answer 195 views
1 answer 214 views
1 answer 178 views
1 answer 175 views
2 answers 193 views
193 views asked Jul 25, 2015 by avibootz
...