Randomaccessfiledemo3~4 reading a set of bytes into a file

Source: Internet
Author: User
Tags readable

randomaccessfiledemo3   write a set of bytes to a file/** *     file operations  ---  randomaccessfile *     *     Randomaccessfile provides a way to write a set of bytes to a file:  *    void write (Byte[] bytesarr)       writes all the bytes in a given byte array one time        similar to this method, there is a common method:      void write (Byte[] bytesarr,int offset,int len)        This method writes out the partial bytes in the given array consecutively based on the position of the current pointer,      this part starts at offset of the array, and it is a continuous Len byte.        other Classes related methods:  sting class   getBytes      API: Use the default character set of the platform to encode this  String  into a  byte  sequence and store the results in a new  byte  array.       my understanding:  according to the system default character set, in this case the string "My life is Wonderful"       follow, Utf-8 each Chinese 3 bytes, which ultimately requires 21 bytes. GBK 2 bytes per Chinese. Eventually 14 bytes are required .      that is, the total 14 bytes in memory (14*8) bits are all written on the number. Converts these numbers to an array of type Byte to save.  */
 Public classRandomAccessFileDemo3 { Public Static voidMain (string[] args)throwsIOException {//Create a file-access-based read-write mode randomaccessfile we only need to pass in "RW" in the second parameter. Randomaccessfile Rafdemo =NewRandomaccessfile ("RafDemo.txt", "RW"); //then the access to the file using Randomaccessfile is readable and writable. String str= "My life is Wonderful";//Write this string        /*Let the computer translate this string into an array of 14 "int" numbers first!         * Converts a string into a set of bytes according to the system default character set (a set of bytes converted into an array of type int "Low 8-bit") * Converts a string into bytes using the GetBytes method of the String class. */        byte[] Strbytes =str.getbytes (); /*byte[] strbytes = str.getbytes ("UTF-8") * means conversion with UTF-8 encoding, one Chinese three bytes*/        /** Write out all bytes in a given byte array once * void Write (byte[] bytesarr) * void Write (byte[] Bytesarr,int offset,in         T len) * The following uses the overloaded method of write, which is the location of the 8th byte. */Rafdemo.write (Strbytes,0,8); //content in a text file: my goodness.        /*Prompt Statement*/System.out.println ("Write Complete"); //release resources must be closed after read and write is completeRafdemo.close (); }}
RandomAccessFileDemo4 method of bulk reading bytes from a file/** * File Operation---randomaccessfile * * Randomaccessfile provides a way to bulk read bytes from a file: *      int read (byte[] Bytesarr) This method attempts to read from a file up to a maximum of bytes of the total length of the given array, starting at the first position of the given byte array, storing the read byte order into the array, and returning the value to the actual amount of bytes read. Similar to this approach, there is a common approach: other classes involved: The getBytes of the Sting class
 Public classRandomAccessFileDemo4 { Public Static voidMain (string[] args)throwsIOException {//Create a file-access-based read-write mode randomaccessfile we only need to pass in "RW" in the second parameter. Randomaccessfile Rafdemo =NewRandomaccessfile ("RafDemo.txt", "R"); //then the access to the file using Randomaccessfile is readable and writable.         byte[] Bytearr =New byte[100]; /*this byte array, which is used to hold the bytes of the Rafdemo file, with a maximum length of 100 bytes per read*/        /** int Read (byte[] d) * One-time attempt to read the total length of the given byte array (bytearr) bytes, * and the amount of bytes read into this array, the return value is actually read to the amount of bytes, if the return value is         -1, * means that the end of the file is read (or nothing is read at this time). */        intLen =Rafdemo.read (Bytearr); System.out.println ("The Rafdemo file occupies bytes:" +len+ "bytes"); //the Rafdemo file occupies a byte of: 8 bytes (Rafdemo content is 4 characters)        /*Prompt Statement*/System.out.println ("Read Complete"); //release resources must be closed after read and write is completeRafdemo.close (); }}

Randomaccessfiledemo3~4 reading a set of bytes into a file

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.