RandomAccessFile basic usage

There are many types of IO can help us write data to a file, FileWriter, FileOutputStream and a variety of decorated class. Which can help us achieve RandomAccessFile write to the specified location.

From the literal meaning, we can carry out random access to files through this class.

 

RandomAccessFile object contains a pointer, when a new object is RandomAccessFile, the pointer to the beginning of the file, i.e., the position of byte 0, when the read / write n bytes, the pointer to the n, in addition , the pointer can be moved to a designated position in accordance with freedom.

 

RandomAccessFile have two constructors, by passing a File object configuration parameter, or by specifying a file name String, also need to specify a parameter MODE, RandomFileAccess designated access mode, "r" is read-only, "rw" to write .

The following code, can understand the basic usage RandomAccessFile class.

Another thing to note is this:

RandomAccessFile the write (byte [] arr) method default encoding format parameter array is "ISO-8859-1", and the default encoding format Chinese GBK, String of getBytes () method of encoding format defaults to "ISO-8859-1" Therefore when writing Chinese, you need this line

String.getBytes ( "GBK"), the encoding format unification!

. 1  / ** 
2  * a RandomAccessFile: access class files anywhere
 . 3   * / 
. 4  public  class RandomAccessFileTest {
 . 5      public  static  void main (String [] args) {
 . 6          insertAtPos ( new new File ( "F.: /Java.txt") , 8 "sea pearl tears \ r \ n Lam Warm Jade smoke \ r \ n" );
 . 7  //         appendContent (new new File ( "F.: /java.txt ')); 
8      }
 . 9  
10  
. 11      / / use example 
12 is      public  static  void test1 () {
 13 is          the try (
 14                 Raf = a RandomAccessFile
 15                          new new a RandomAccessFile ( "F.: / Code for SE / codeByVi / the src / COM / VI / IO / RandomAccessFileTest.java", "R & lt" );
 16          ) {
 . 17              // get the current pointer position 
18 is              Long index = Raf .getFilePointer ();
 . 19              System.out.println ( "current pointer location:" + index);
 20 is              // position setting pointer 
21 is              raf.seek (300 );
 22 is              System.out.println ( "present position of the pointer : "+ raf.getFilePointer ());
 23 is              int hasRead = 0 ;
 24              byte [] Buffer = new new byte [1024 ];
 25              the while ((hasRead = raf.read (Buffer)) = -1! ) {
 26 is                  of System.out.print ( new new String (Buffer, 0 , hasRead));
 27              }
 28          } the catch (Exception E ) {
 29              e.printStackTrace ();
 30          }
 31 is      }
 32  
33 is      / ** 
34 is       * append the contents of file
 35       * / 
36      public  static  void appendContent (file file) {
 37 [          the try (
 38 is                 Raf = a RandomAccessFile new new a RandomAccessFile (File, "RW" );
 39          ) {
 40              raf.seek (raf.length ());
 41 is              String Content = new new String ( "Remembrance into" .getBytes ( "GBK") , "ISO8859-1" );
 42              raf.write ( "three lines must be my teacher" .getBytes ( "GBK" ));
 43              System.out.println ( "additional success!" );
 44          } the catch (Exception E ) {
 45              e.printStackTrace ();
46         }
47     }
48 
49     /**
50      * Insert the specified position specified string file
 51 is       * @param File
 52 is       * @param POS
 53 is       * @param Content
 54 is       * / 
55      public  static  void insertAtPos (File File, int POS, String Content) {
 56 is          the try (
 57 is                  a RandomAccessFile Raf = new new a RandomAccessFile (File, "RW" );
 58                  the FileInputStream fr = new new the FileInputStream ( new new File ( "F.: /temp.txt" ));
 59                 BW = a FileOutputStream new new a FileOutputStream ( new new File ( "F.: /Temp.txt" ));
 60          ) {
 61 is              raf.seek (POS);
 62 is              byte [] Buffer = new new  byte [64 ];
 63 is              int hasRead = 0 ;
 64              // the contents of the specified location to be written to a temporary file 
65              the while ((hasRead = raf.read (Buffer)) = -1! ) {
 66                  bw.write (Buffer, 0 , hasRead);
 67              }
 68              / / reposition 
69              raf.seek (POS);
 70             // target content written to the destination file 
71 is              raf.write ( "sea pearl tears" .getBytes ( "GBK" ));
 72              // write the contents of the temporary file to the destination file's end 
73 is              the while (! (hasRead = fr.read (Buffer)) = -1 ) {
 74                  raf.write (Buffer, 0 , hasRead);
 75              }
 76              new new File ( "F.: /temp.txt" ) .Delete ();
 77              System.out.println ( "write completion!" );
 78          } the catch (Exception E) {
 79              e.printStackTrace ();
 80          }
 81      }
 82 }

 

Guess you like

Origin www.cnblogs.com/blogforvi/p/11780054.html