Hi everybody,
This project is a bit beyond the scope of my class right now, but it's relevant to what we are working on and I was interested so I figured I'd give it a shot. I'm trying to generate 100 random numbers and write them to a .txt file (to be used in another project I've written). Here is my code:
import java.io.*; import java.util.Random; public class intToFile_main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub generateFile("C:\\CSEclipseWorkspace\\intToFile\\numbers.txt"); } private static void generateFile(String fileName) { try{ FileOutputStream fos = new FileOutputStream(fileName); DataOutputStream dos = new DataOutputStream(fos); Random generator = new Random(); for (int i = 0; i <= 99; i++) { int num = generator.nextInt(99) + 1; //generate a random number dos.writeInt(num); //write the number to the file } dos.close(); } catch (IOException e) { e.printStackTrace(); } } }
As far as I can tell, it's working, except that rather than displaying numbers, the document reads something like this (it's different every time because of the random number generator):
Does anyone have any idea as to what's going on and how I can fix it? Thanks very much!? > 8 0 S ( " _ Y 9 ( P R P ( @ W % W ? ? ; = C - K > S O 2 a \ L O U " X a
Y $ ( + \ [ 7 # ? & # W c a X ; @ / 5 V / b I : V b G O $ Z 6 2 9 S -