KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > io > RandomAccessFile

java.io
Class RandomAccessFile

java.lang.Object
  extended by java.io.RandomAccessFile
All Implemented Interfaces:
Closeable, DataInput, DataOutput
See Also:
Top Examples, Source Code

public void close()
           throws IOException
See Also:
Closeable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[893]_
By Anonymous on 2004/12/21 14:46:03  Rate
import java.io.*; 
   
 public class foo  {  
   
     public static void main ( String [  ]  args )  throws IOException  {  
         RandomAccessFile raf = new RandomAccessFile ( "foo.txt", "rw" ) ; 
   
         try  {  
             Writer out = new OutputStreamWriter ( new FileOutputStream ( raf.getFD (  )  ) , "UTF-8" ) ; 
   
             out.write ( "Hello World!!" ) ; 
             out.flush (  ) ; //flush the stream before seeking to a new position  
   
             raf.seek ( 6 ) ; // Just before the 'W' 
             out.write ( "Java" ) ; //overwites "World" with "Java" 
             out.flush (  ) ; 
          }  
         finally  {  
             raf.close (  ) ; 
          }  
      }  
  }  
 


[1196]_
By Chris Johnson on 2005/02/02 18:50:25  Rate
For the example already given.. "Java" would overwrite "Worl" not "World".

[1695]java file access
By mbiis { at } fsmail { dot } net on 2006/01/08 18:59:33  Rate
import java.io.*; 
 import java.util.*; 
  
  
 public class database 
  {  
   String [  ]  [  ]  contact; 
   int position = 0; 
  
  
   public database ( int noOfContact )  
    {  
     contact = new String [ noOfContact ]  [ noOfContact ] ; 
    }  
  
  
   public void setDetails ( String name, String tel, String exten )  
    {  
     //Using the args place the data into 
     //to array with given current position. 
     //Increment the position by one 
    }  
  
  
   public void setName (  String name, int pos )  
    {  
     //set the name of entry with a given position 
    }  
  
  
   public void setTelepehone ( String tel, int pos )  
    {  
     //set the telephone number of an entry with given position 
    }  
  
  
   public void setExtension ( String exten, int pos )  
    {  
     //set extenstion number of an entry with given position 
    }  
  
  
   public void setNumbers ( String num, int pos )  
    {  
     //set addition number for this entry 
     //with given position 
    }  
  
  
   public String getFullName ( int pos )  
    {  
     //return the full name of the entry with given position 
     return ""; 
    }  
   public String getTelephone ( int pos )  
    {  
     //return telephone number of entry with given position 
     return ""; 
    }  
  
  
   public String getExtenstion ( int pos )  
    {  
     //return extension number of entry with given position 
     return ""; 
    }  
  
  
   public String [  ]  getDetails ( int pos )  
    {  
     //create an array of position depending on the entry information 
     //data type of string 
     //store information about the entry 
     //in each array with a given position 
     //return the array 
     return null; 
    }  
  
  
   public int searchByName ( String s )  
    {  
     //this method returns the position of the array 
     //where the contact is being held 
     return 0; 
    }  
  
  
   public int searchByNumber ( String num )  
    {  
     //returns the position of this contact with given 
     //number 
     return 0; 
    }  
  
  
   public int searchByEx ( String ex )  
    {  
     //returns the position of this contact with given 
     //extenstion number 
     return 0; 
    }  
  
  
   public String [  ]  [  ]  getAllContacts (  )  
    {  
     return contact; 
    }  
  
  
   public int getPos ( int tel )  
    {  
     //get the position of contact with a given telephone number 
     //remember to convert the int to string 
     //then query the arrays 
     return 0; 
    }  
  
  
   public boolean save ( String file )  
    {  
     boolean b = false; 
     //Save the 2d array into a flat file 
     //with given file name 
     //return b as true if save is successfull 
  
  
     return b; 
    }  
  
  
   public boolean load ( String file )  
    {  
     boolean b = false; 
     //load each contact and save into 
     //3d array with given file name to load from 
     //providing feedback to illistrate that the file has been loaded successfully 
     return b; 
    }  
  
  
  } 


[1796]copy a part of a file to another binary file
By ab on 2006/07/31 09:38:06  Rate
// copy bytes  [ beginn,end )  from input file to output file:                                                                                                                                                                    
     RandomAccessFile in = new RandomAccessFile ( fname_in, "r" ) ; 
     RandomAccessFile out= new RandomAccessFile ( fname_out, "rw" ) ;                                                                                                                     
     int len = ( int )  in.length (  ) ; // not used 
     int begin=...;                                                                                                                                                               
     int end=...;                                                                                                                                                               
     int buf_sz=1024*1024;                                                                                                                                                       
     byte buf [  ]  = new byte [ buf_sz ] ;                                                                                                                                         
     while  ( begin < end )   {                                                                                                                                                            
         int cp_len=end-begin;                                                                                                                                                     
         if  ( cp_len > buf_sz )  cp_len=buf_sz;                                                                                                                                         
                                                                                                                                                                     
         in.seek ( begin ) ;                                                                                                                                                            
         in.readFully ( buf,0,cp_len ) ; // read cp_len bytes                                           
         out.write ( buf,0,cp_len ) ;                                                                                                                                                  
         begin+=cp_len;                                                                                                                                                              
      }                                                                                                                                                                            
     in.close (  ) ;                                                                                                                                                                               
     out.close (  ) ;                                                                                                                                                                               
 


[1800]read some bytes from input file and append to output file
By Anonymous on 2006/08/05 09:29:48  Rate
// copy bytes  [ beginn,end )  from input file to output file: 
 // parameters: infile outfile start end 
 public static void main ( String [  ]  args )  throws IOException, NumberFormatException  {  
   String infile=args [ 0 ] ; 
   String outfile=args [ 1 ] ; 
   int begin=Integer.parseInt ( args [ 2 ]  ) ; 
   int end=Integer.parseInt ( args [ 3 ]  ) ; 
   RandomAccessFile in = new RandomAccessFile ( infile, "r" ) ; 
   RandomAccessFile out= new RandomAccessFile ( outfile, "rw" ) ;                                                                                                                     
   int len =  ( int )  in.length (  ) ; // get length of input file 
   int len2=  ( int )  out.length (  ) ; // get length of output file 
   out.seek ( len2 ) ; // goto end of output file 
   int buf_sz=1024*1024;                                                                                                                                                       
   byte buf [  ]  = new byte [ buf_sz ] ;                                                                                                                                         
   while  ( begin < end )   {                                                                                                                                                            
       int cp_len=end-begin;                                                                                                                                                     
       if  ( cp_len > buf_sz )  cp_len=buf_sz;                                                                                                                                         
                                                                                                                                                                   
       in.seek ( begin ) ;                                                                                                                                                            
       in.readFully ( buf,0,cp_len ) ; // read cp_len bytes into buffer                                           
       out.write ( buf,0,cp_len ) ; // write bytes                                                                                                              
       begin+=cp_len;                                                                                                                                                              
    }                                                                                                                                                                            
   in.close (  ) ;                                                                                                                                                                               
   out.close (  ) ;                                                                                                                                                                               
  }   
 


public final FileChannel getChannel()
See Also:
getFilePointer, position
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final FileDescriptor getFD()
                           throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long getFilePointer()
                    throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long length()
            throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public RandomAccessFile(File file,
                        String mode)
                 throws FileNotFoundException
See Also:
FileDescriptor
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public RandomAccessFile(String name,
                        String mode)
                 throws FileNotFoundException
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityManager.checkRead(java.lang.String), SecurityException, IllegalArgumentException, FileDescriptor
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[419]_
By Anonymous on 2004/01/20 23:18:08  Rate
RandomAccessFile f = new RandomAccessFile ( "A:\\conexion.txt", "r" ) ; 
 


[772]Append to a file
By JavaJeffG on 2004/09/01 06:52:13  Rate
// Opens file in read/write mode  ( "rw" ) .   
 // Appends requests at end of file. 
 RandomAccessFile raf = new RandomAccessFile ( "OutputFile.txt", "rw" ) ; 
 raf.seek ( raf.length (  )  ) ; // Jump to EOF. 
 ... 
 raf.close (  ) ;


public int read()
         throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read(byte[] b)
         throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read(byte[] b,
                int off,
                int len)
         throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean readBoolean()
                          throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final byte readByte()
                    throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final char readChar()
                    throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final double readDouble()
                        throws IOException
See Also:
Double.longBitsToDouble(long), readLong(), EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final float readFloat()
                      throws IOException
See Also:
Float.intBitsToFloat(int), readInt(), EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void readFully(byte[] b)
                     throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int readInt()
                  throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final String readLine()
                      throws IOException
See Also:
DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final long readLong()
                    throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final short readShort()
                      throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int readUnsignedByte()
                           throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int readUnsignedShort()
                            throws IOException
See Also:
EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final String readUTF()
                     throws IOException
See Also:
readUnsignedShort(), UTFDataFormatException, EOFException, DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void seek(long pos)
          throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setLength(long newLength)
               throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1345]Skip some part of fixed bytes from the ending portion of the file
By Anonymous on 2005/03/14 01:48:56  Rate
RandomAccessFile raf = new RandomAccessFile ( "RandFile.txt", "rw" ) ; 
 //getting the file length 
 long length = raf.length (  ) ; 
 System.out.println ( "File Length="+raf.length (  )  ) ; 
 //reducing the length by 8.This is useful when you want to  
 //skip some part of fixed bytes from the ending portion of the file 
 //here it is 44 
 raf.setLength ( length - 44 ) ; 
 System.out.println ( "File Length="+raf.length (  )  ) ; 
 


public int skipBytes(int n)
              throws IOException
See Also:
DataInput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void write(byte[] b)
           throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void write(int b)
           throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeBoolean(boolean v)
                        throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeByte(int v)
                     throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeBytes(String s)
                      throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeChar(int v)
                     throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeChars(String s)
                      throws IOException
See Also:
writeChar(int), DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeDouble(double v)
                       throws IOException
See Also:
Double.doubleToLongBits(double), DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeFloat(float v)
                      throws IOException
See Also:
Float.floatToIntBits(float), DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeInt(int v)
                    throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeLong(long v)
                     throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeShort(int v)
                      throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void writeUTF(String str)
                    throws IOException
See Also:
DataOutput
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags