Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,849 questions

9,751 answers

573 users

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
...