Java I / O -- RandomAccessFile class (read and write of random access file)

Keywords: Java JDK

1. Description of RandomAccessFile class in JDK API

 

Instances of this class support reading and writing to random access files. Random access to a file behaves like a large array of bytes stored in the file system. There is a cursor or index to the implicit array, which is called a file pointer; the input operation reads bytes from the file pointer and moves the file pointer forward as the bytes are read. If a random access file is created in read / write mode, the output operation is also available; the output operation writes bytes from the file pointer and moves the file pointer forward as bytes are written. An output operation after writing the current end of an implied array causes the array to expand. The file pointer (realizing random reading and writing of array) can be read by getFilePointer method and set by seek method.

Typically, if all of the read routines in this class have reached the end of the file before reading the required number of bytes, an EOFException is thrown. If for some reason no bytes can be read, instead of reaching the end of the file before reading the required number of bytes, an IOException is thrown instead of an EOFException. In particular, if the stream is closed, an IOException may be thrown.

2.RandomAccessFile demonstration

  1 public class RandomAccessFileDemo {
  2 
  3 	/**
  4 	 * @The code comes from Podcast
  5 	 * @param args
  6 	 * @throws IOException
  7 	 */
  8 	public static void main(String[] args) throws IOException {
  9 
 10 
 11 		/*
 12 		 * RandomAccessFile:
 13 		 * characteristic:
 14 		 * 1,Only files can be manipulated.
 15 		 * 2,Can read and write.
 16 		 * 3,A byte array is maintained. Internal definition of byte stream read and write.
 17 		 * 4,Through the operation of the pointer, we can read and write to any position of the file.
 18 		 */
 19 
 20 
 21 //		writeFile();
 22 		readFile();
 23 	}
 24 
 25 	public static void readFile() throws IOException {
 26 
 27 		RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "r");
 28 
 29 		//Random reading, just by setting the position of the pointer.
 30 		raf.seek(8*1);
 31 
 32 		byte[] buf = new byte[4];
 33 		raf.read(buf);
 34 		String name = new String(buf);
 35 
 36 		int age = raf.readInt();
 37 
 38 		System.out.println(name+":"+age);
 39 
 40 		raf.close();
 41 
 42 
 43 
 44 	}
 45 
 46 	public static void writeFile() throws IOException {
 47 
 48 		//1. Create an object with random access to the file. If the file does not exist, it is created; if it exists, it is not created and not overwritten.
 49 		RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "rw");
 50 
 51 		//2. Write the name and age.
 52 //		raf.write("Zhang San". getBytes());
 53 //		raf.writeInt(97); / / guarantees the byte integrity of the integer.
 54 //		raf.write("Li Si". getBytes());
 55 //		raf.writeInt(99); / / guarantees the byte integrity of the integer.
 56 
 57 		//3. Random write.
 58 		raf.seek(8);//Sets the position of the pointer.
 59 		raf.write("Wang Wu".getBytes());
 60 		raf.writeInt(100);
 61 		System.out.println(raf.getFilePointer());
 62 
 63 		raf.close();
 64 
 65 
 66 	}
 67 
 68 }

2017-12-31 content from API and podcast

Posted by piksimoon on Sun, 03 May 2020 04:23:40 -0700