All pastes #979272 Raw Edit

Stuff

public java v1 · immutable
·published 2008-04-10 06:23 UTC
rendered paste body
/* * Creates 20 random integers (1-50) then writes them to a file. * Then it reads the file, and outputs them. */import java.util.*;import java.io.*;public class Program8{    public static void main(String[] args)    {        // Create a RNG, and an byte array.        Random gen = new Random();        byte[] matrix = new byte[20];                // Creates a file, then has it setup to be removed on exit        // and creates the actual file.        File f = new File("data.bin");        try        {            f.deleteOnExit();            f.createNewFile();        }        catch(IOException e)        {            System.out.println(e.getMessage());        }        try        {            // create a file output stream for File f            FileOutputStream out = new FileOutputStream(f);            System.out.println("WRITING DATA TO FILE ("+ f.getName() +"):");            for (int x=0; x<20; x++)             {               // store each integer as a byte               // then display byte as integer               matrix[x] = (byte)(1+gen.nextInt(50));               System.out.print((int)matrix[x] + " ");            }            try            {                // write out the byte information to the file                for (int x=0; x<matrix.length;x++)                {                    out.write(matrix[x]);                }            }            catch(IOException e)            {                System.out.println(e.getMessage());            }        }        catch(FileNotFoundException e)        {            System.out.println(e.getMessage());        }        System.out.println();        try        {            // create a file input stream for File f            FileInputStream in = new FileInputStream(f);            System.out.println("READING DATA FROM FILE ("+ f.getName() +"):");            try            {                for (int x=0;x<20;x++)                {                    // read in one byte from file                    // then display each as integer                    System.out.print((int)(in.read()) + " ");                }            }            catch(IOException e)            {                System.out.println(e.getMessage());            }        }        catch(FileNotFoundException e)        {            System.out.println(e.getMessage());        }            }}