Three use of the Randomaccessfile class
The Randomaccessfile class is a Java-provided access class for the contents of a file, which can be either read or write files. Randomaccessfile class
Support random access to files, can access any location of the file.
How to construct the Randomaccessfile class:
Methods in the Randomaccessfile class:
(1) Java file model
The file on the hard disk is a byte byte stored in byte and is a collection of data.
(2) Open file
There are two modes of "RW" (Read-write), "R" (read-only). For example, the following constructs:
Randomaccessfile RAF = new Randomaccessfile (file, "RW");
The file pointer opens the file when the pointer is at the beginning, i.e. pointer=0;
(3) How to write raf.write (int)---> writes only one byte (the last 8 bits), and the pointer points to the next position, ready to write again.
(4) method of reading
int b=raf.read ()---> Read a byte
(5) Be sure to close the file when it is read and written (Oracle official note).
Instance code:
<span style= "FONT-SIZE:18PX;" >import java.io.*;import java.util.*;p ublic class rafdemo{public static void Main (string[] args) throws ioexception{ File Demo = new file ("E:\\java\\javase\\io\\demo"), if (!demo.exists ()) {Demo.mkdir ();} File File = new file (demo, "Raf.dat"), if (!file.exists ()) {file.createnewfile ();} Read and write operations Randomaccessfile RAF = new Randomaccessfile (file, "RW");//Pointer position System.out.println ("the initial position of the pointer not written before:" + Raf.getfilepointer ()); Raf.write (' a ');//write only one byte System.out.println ("pointer position after first write:" +raf.getfilepointer ()); Raf.write (' B '); int i = 0x7fffffff;//Use the Write () method to write only one byte at a time, if you want to write I will have to write 4 times raf.write (i >>> 24);//high 8-bit raf.write (i >>> Raf.write (i >>> 8); Raf.write (i); SYSTEM.OUT.PRINTLN ("The pointer position after the sixth write:" +raf.getfilepointer ());//You can write a intraf.writeint (i) directly;//A Chinese occupies 2 bytes of string s = "Medium"; byte[] GBK = s.getbytes ("GBK"); Raf.write (GBK); System.out.println ("The byte length after the eighth write is:" +raf.length ());//Read the file, you must move the pointer to the head raf.seek (0);//One-time read, read the contents of the file into a byte array byte[] buf = New byte[(int) raf.length ()];raf.read (buf);//Output System.out.println (arrays.tostring (BUF)) as a string, or output for (byte b:buf) {System.out.print (Integer.toh) in 16-binary mode Exstring (b & 0xff) + "");} Close read and write operations Raf.close ();}} </span>
Operation Result:
four usage of byte stream
The IO stream in Java is an input/output stream, which is also divided into byte stream and character stream.
(1) byte stream 1) InputStream abstract class and OutputStream abstract class
The InputStream abstract class abstracts the way the application reads the data, and the OutputStream abstract class abstracts the way the application writes out the data.
2) EOF = end means read-1 reading to the end 3) Input stream basic method
int b = In.read ();//read one byte unsigned fill to int low eight bits. -1 is EOF
In.read (byte[] buf)
In.read (byte[] buf,int start,int size)
4) Basic method of output stream
Out.write (int b)//write a byte to stream, B's low 8 bits
Out.write (byte[] buf)//writes BUF byte array to the stream
Out.write (byte[] buf,int start,int size)//byte array buf writes a size-length byte to the stream starting from the start position
5) FileInputStream class---> Specifically read the data on the file
The FileInputStream class inherits the InputStream abstract class.
Methods in the FileInputStream class:
Example code 1:
<span style= "FONT-SIZE:18PX;" >import java.io.*;p ublic class ioutil{/* * Reads the contents of the specified file, follows the hexadecimal output to the console * and each output 10 byte newline * Single byte read not suitable for large files, large file efficiency is very low */public Stati c void Printhex (String fileName) throws ioexception{//reads the file as a byte stream fileinputstream in = new FileInputStream (fileName); int b; int i = 1;while ((b = in.read ())!=-1) {if (b <= 0xf) {//single number preceded by 0system.out.print ("0");} Converts integer b to a hexadecimal-represented string System.out.print (integer.tohexstring (b) + " "), if (i++%10==0) {System.out.println ();}} Close read-write stream in.close ();} public static void Main (string[] args) {Try{ioutil.printhex ("E:\\java\\javase\\io\\fileutils.java");} catch (IOException e) {e.printstacktrace ();}}} </span>
Operation Result:
Example code 2:
<span style= "FONT-SIZE:18PX;" >import java.io.*;p ublic class ioutil{/* * Read the contents of the specified file, follow the hexadecimal output to the console * and each output 10 byte newline * Single byte read not suitable for large files, large file efficiency is very low */PUBL The IC static void Printhex (String fileName) throws ioexception{//reads the file as a byte stream fileinputstream in = new FileInputStream ( FILENAME); int b; int i = 1;while ((b = in.read ())!=-1) {if (b <= 0xf) {//single number preceded by 0system.out.print ("0");} Converts integer b to a hexadecimal-represented string System.out.print (integer.tohexstring (b) + ""), if (i++%10==0) {System.out.println ();}} Close read-write stream in.close ();} public static void Printhexbybytearray (String fileName) throws Ioexception{fileinputstream in = new FileInputStream ( FileName); byte[] buf = new BYTE[20 * 1024];/* Bulk Read bytes from in, put into buf this byte array, * Starting from No. 0 position, up to Buf.length * Returns the number of bytes read */int byt Es = In.read (buf,0,buf.length);//Read all at once, indicating that the byte array is large enough int j = 1; for (int i = 0; i < Bytes;i.print ("0"), if (Buf[i] <= oxf) {system.out}system.out.print (integer.tohexstring (buf[i) & Amp 0xFF) + ""); if (j++%10==0) {System.out.println ();}} In.cloSe ();} public static void Main (string[] args) {Try{ioutil.printhexbybytearray ("E:\\java\\javase\\io\\fileutils.java");} catch (IOException e) {e.printstacktrace ();}}} </span>
Run result: Same as the result of instance code 1.
What if the byte array is not large enough to read the file at once?
Example Code 3:
<span style= "FONT-SIZE:18PX;" >import java.io.*;p ublic class ioutil{/* * Read the contents of the specified file, follow the hexadecimal output to the console * and each output 10 byte newline * Single byte read not suitable for large files, large file efficiency is very low */PUBL The IC static void Printhex (String fileName) throws ioexception{//reads the file as a byte stream fileinputstream in = new FileInputStream ( FILENAME); int b; int i = 1;while ((b = in.read ())!=-1) {if (b <= 0xf) {//single number preceded by 0system.out.print ("0");} Converts integer b to a hexadecimal-represented string System.out.print (integer.tohexstring (b) + ""), if (i++%10==0) {System.out.println ();}} Close read-write stream in.close ();} public static void Printhexbybytearray (String fileName) throws Ioexception{fileinputstream in = new FileInputStream ( FileName); byte[] buf = new byte[8 * 1024];int bytes = 0; int j = 1; while ((bytes = In.read (buf,0,buf.length))!=-1) {for (int i = 0; i < bytes;i++) {//byte type 8-bit, int type 32-bit//To avoid data conversion errors by &oxff will be high 24 bits clear 0 System.out.print (integer.tohexstring (Buf[i] & 0xff) + ""); if (j++%10==0) {System.out.println (); }}} in.close ();} public static void Main (string[] args) {Try{iouTil.printhexbybytearray ("E:\\java\\javase\\io\\fileutils.java");} catch (IOException e) {e.printstacktrace ();}}} </span>
Operation Result:
Javase Getting Started learning 43: I/O stream of File Transfer Basics (ii)