"Javase" Day08_randomaccessfile

Source: Internet
Author: User

"Javase" Day08_randomaccessfile


1) java.io.RandomAccessFile

* This class is used to read and write file data.

* There are two modes of creating this class:

* 1: Read-only (R), read only for files

* 2: Read/write (rw), read/write to file data


2) Common construction methods:

* Randomaccessfile (File file,string mode)

* Randomaccessfile (String path,string mode)


3) void write (int i)

* This method writes a byte to the file, which is a 8-bit binary

* Written in binary form of the int value "low Eight bits"


Code Demo:

Package Day01;import java.io.ioexception;import java.io.randomaccessfile;/** * Java.io.RandomAccessFile * This class is used to read and write file data. * There are two modes of creating this class: * 1: Read only, read only to file * 2: Read and write, read and write to file data  * * Common construction Method: * Randomaccessfile (file file,string mode) * Randomaccessfile (String path,string mode) */public class RandomAccessFileDemo1 {public static void main (string[] args) throws IOException {/* * operation of the file Test.dat file under the root directory of the current project */randomaccessfile RAF = new Randomaccessfile ("Test.dat", "RW");/* * void write (int i) * the Method writes out a byte in the file, that is, the 8-bit binary * is written in the binary form of the int value "low Eight bits" */raf.write ( -1); Raf.close ();}}

4) int Read ()

* Reads this byte from the RAF current pointer position, and returns 1 if the end of the file is read.

Code Demo:

Package Day01;import Java.io.filenotfoundexception;import Java.io.ioexception;import java.io.randomaccessfile;/** * Reads a byte * */public class RandomAccessFileDemo2 {public static void main (string[] args) throws IOException {/* * Read Test.dat text The first byte in the */randomaccessfile RAF = new Randomaccessfile ("Test.dat", "R");/* * int Read () * reads the byte from the RAF current pointer position, and returns 1 if it is read to the end of the file. */int a = Raf.read (); System.out.println (a); Raf.close ();}}

5) void write (byte[] data)

* Writes all the bytes in a given byte array once.

See the code for specific details and additional information:

Package Day02;import java.io.ioexception;import Java.io.randomaccessfile;import java.util.arrays;/** * Bulk Read/write Data * *void Write (byte[] data) * Writes out all the bytes in a given byte array at once. */public class RandomAccessFileDemo1 {public static void main (string[] args) throws IOException {Randomaccessfile RAF = NE W randomaccessfile ("Demo.txt", "RW"); String str = "Friction friction, is the devil's pace."; * * Converts a string to a set of bytes according to the current system default character set. * String provides a method: * byte getBytes () *  Common Character Set: * GBK: GB Code, Chinese 2 bytes, English 1 bytes * UTF-8: Commonly known as the Universal Code, Chinese 3 bytes, English 1 bytes *         This encoding set 1-4 bytes constitute a different country Language text * iso8859-1: A common European character set, does not support Chinese. *  * String provides an overloaded GetBytes () method that allows the current string to be converted to the corresponding byte based on the given character set. * byte[] GetBytes (String charsetname) *  */byte[] data = str.getbytes ("GBK");//byte[] data = Str.getbytes (); SYSTEM.OUT.PRINTLN (data);//Writes all bytes in the given byte array once to the file. Raf.write (data);//raf.write ( -26); Raf.close ();}}

6) int read (byte[] data)

* One-time attempt to read the bytes of the length of the given byte array data and store them in an array. The return value is the amount of bytes actually read, and if the return value is-1, it reads to the end of the file (EOF end of files).

See the code for details:

Package Day02;import Java.io.ioexception;import java.io.randomaccessfile;/** * Bulk Read bytes * */public class RandomAccessFileDemo2 {public static void main (string[] args) throws IOException {Randomaccessfile RAF = new RANDOMACCESSF Ile ("Demo.txt", "RW");/* * int read (byte[] data) * A one-time attempt to read the bytes of the length of the given byte array data and save those * to the array. The return value is the actual read to the amount of bytes, if the return value is-1, indicating that * read to the end of the file (EOF end of files) */byte[] data = new Byte[50];int len = raf.read (data); System.out.println ("actually read:" +len+ "bytes");/* * String provides a construction method that allows us to convert bytes in a given * byte array to the corresponding string. *  * string (byte[] data) * Converts all bytes in a byte array to a string according to the current system default character set. *  * String (byte[] data,int start,int Len) * takes the bytes starting at start in the current byte array, successive Len, * then converts them to a string as the current system default character set. *  * string (byte[] data,int start,int len,string charsetname) * Converts part of a byte array to the corresponding string in the given character set. */string str = new String (Data,0,len, "GBK"); System.out.println (str); Raf.close ();}}

7) long Getfilepointer ()

* Gets the pointer position of the current randomaccessfile (points to the number of bytes in the file)

* Just created the RAF by default points to the first byte of the file. The subscript starts from 0.

Code Demo:

Package Day02;import Java.io.ioexception;import java.io.randomaccessfile;/** * Randomaccessfile is always read or write based on the current position of the pointer. * The pointer will automatically move backwards whenever the current byte is read or written. * * */public class RandomAccessFileDemo3 {public static void main (string[] args) throws IOException {Randomaccessfile RAF = new Randomaccessfile ("Raf.dat", "RW");/* * Long getfilepointer () * Gets the pointer position of the current Randomaccessfile * (points to the number of bytes of the file) * Just created the RAF Point to the first byte of the file. * Subscript starting from 0. */long pos = Raf.getfilepointer (); System.out.println (POS); 0//writes a byte of Raf.write (1);p OS = Raf.getfilepointer (); System.out.println (POS); 1//write an int maximum value//01111111 11111111 11111111 11111111int max = Integer.max_value;raf.write (max>>>24); Raf.write (max>>>16); Raf.write (max>>>8); Raf.write (max);p OS = Raf.getfilepointer (); System.out.println (POS); 5/* * void writeint (int i) * writes out 4 bytes in a row and writes out the given int value in full. * Methods for writing other types of data are also available. */raf.writeint (Integer.max_value); 4 bytes of Raf.writelong (123L); 8 bytes of raf.writedouble (123.123); 8 bytes, float-4 bytes pos = raf.getfilepointer (); System.out.printlN (POS); 25/* * Read the Long value written above, you need to first move the pointer to the first byte of the * Long value, and then read out 8 bytes in a row to read it back. * Void Seek (long POS) * This method moves the pointer to the specified position. */raf.seek (9);/* * Long Readlong () * starts at the current position of the pointer, reads 8 bytes in a row * and converts to a long value. */long num = Raf.readlong (); SYSTEM.OUT.PRINTLN ("num:" +num); Num:123pos = Raf.getfilepointer (); System.out.println (POS); 17raf.close ();}}

8) Copy File demo

Package Day02;import Java.io.ioexception;import java.io.randomaccessfile;/** * If you want to improve read and write efficiency we need to increase the amount of data per read and write * To reduce the number of read and write hardware to improve. * */public class CopyDemo1 {public static void main (string[] args) throws IOException {randomaccessfile src = new Randomac Cessfile ("Img.jpg", "R"); Randomaccessfile desc = new Randomaccessfile ("Img_c.jpg", "RW"); int len = -1;//The actual byte amount read each time byte[] buf = new byte[1024*10];/ /10KB more reasonable long start = System.currenttimemillis (); while ((Len=src.read (BUF))!=-1) {//desc.write (BUF);// This may cause a lot of garbage data/* * Write out the given byte array starting with 0 consecutive Len bytes. */desc.write (Buf,0,len);} Long end = System.currenttimemillis (); System.out.println ((End-start) + "MS"), Src.close ();d esc.close ();}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Javase" Day08_randomaccessfile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.