Java Tutorial

 
 
 
 
import java.io.*;

class  RWRandInt
{
    public static void main(String[] args) 
    {
           try{
               FileOutputStream fos = new FileOutputStream(\"RandData.txt\");
               DataOutputStream dos = new DataOutputStream(fos);

               for (int i=0;i<20 ;i++ )  //for writing 20 integers to RandData.txt file
               {
                    dos.writeInt((int)Math.random() * 100);
               }
               fos.close();
               dos.close();

               //For Reading Data
               FileInputStream fis = new FileInputStream(\"RandData.txt\");
               DataInputStream dis = new DataInputStream(fis);
               for (int i=0;i<20 ;i++ )
               {
                    int n = dis.readInt();
                   System.out.print(n + \"  \");
               }
               fis.close();
               dis.close();
             }catch(Exception e) {}
    }
}

 
 
Didn't find what you were looking for? Find more on Program of storing and retrieving integers using data streams on a single file