File encoding and the use of Randomaccessfile file streams--io Learning notes (i) __ Code

Source: Internet
Author: User
file Encoding

UTF-8 code: Chinese occupies 3 bytes, English occupies 1 bytes
GBK code: Chinese occupies 2 bytes, English occupies 1 bytes
Utf-16be code: Chinese occupies 2 bytes, English occupies two bytes

Note: The UTF-16BE encoding is the encoding used in the Java double-byte encoding. Java uses a utf-16be way to store data. Eg:string strings are encoded using UTF-16BE.

Package com.test.test; /** * Use three encoding to print the same string, see the difference between how many bytes each encoding uses * */public class Test {public static void main (string[] args) throws E
        xception{String s = "small grey grey ABC";
        ENCODEGBK (s);
        EncodeUTF8 (s);
    Encodeutf16be (s); /** * Using GBK encoding * @param s */private static void Encodegbk (String s) {System.out.print
        ln ("GBK Code:");
        Converts a string to a byte array (by default the encoding format for the conversion of the project setting (which I set here is GBK) byte[] byte1 = S.getbytes (); for (byte b:byte1) {* * * 1.toHexString () method, converts bytes to type int and displays in 16 binary * 2. method to put Byte ( bytes) converted to int type * 3.&0XFF means to take only the lowest 8 digits: because the int type occupies 32 bits (4 bytes) But the byte type occupies 8 bits (1 bytes), * So after converting byte to int, in
        The first 24 digits of the T type will be 0, so you can use the &0xff only to take the 8-bit System.out.print (integer.tohexstring (b & 0xff) + "");
    } System.out.println (); /** * Using UTF-8 encoding * @param s */private static void EncodeUTF8(String s) throws exception{System.out.println ("UTF8 Code:");
        Converts a string to a byte array (by default the encoding format for the conversion of the project setting (which I set here is GBK) byte[] byte1 = s.getbytes ("Utf-8");
        for (byte b:byte1) {System.out.print (integer.tohexstring (b & 0xff) + "");
    } System.out.println (); 
        /** * Using UTF-16BE encoding * @param s */private static void Encodeutf16be (String s) throws exception{
        System.out.println ("Utf16be Code:");
        Converts a string to a byte array (by default the encoding format for the conversion of the project setting (which I set here is GBK) byte[] byte1 = s.getbytes ("Utf-16be");
        for (byte b:byte1) {System.out.print (integer.tohexstring (b & 0xff) + "");
    } System.out.println ();
 }


}

Results screenshot:

You can see the difference in the number of bytes used in both English and Chinese in the three encoding modes from the diagram.

Note: When your byte sequence is some kind of encoding, this time you want to turn the byte sequence into a string, you need to use this encoding, otherwise there will be garbled.
eg

Package com.test.test;


public class Test {public

    static void Main (string[] args) throws exception{
        String s = "Small gray grey abc";
        System.out.println ("UTF8 Code:");
        Converts a string to a byte array (by default the encoding format for the conversion of the project setting (which I set here is GBK)
        byte[] bytes = s.getbytes ("Utf-8");
        for (byte b:bytes) {
            System.out.print (integer.tohexstring (b & 0xff) + "");
        }
        System.out.println ();
        String str = new string (bytes);//Here is the encoding format for the project settings (I set the GBK encoding)
        System.out.println (str);
    }


Results screenshot:

Here you can know that if you want to appear the original result must be converted to the original encoding.
So the code for the top construction string should be modified to:
String str = new string (bytes, "Utf-8");
The correct result will appear:
randomaccessfile file stream:

model for Java files:
The file on your hard disk is stored in byte byte and is a collection of data.
1. Introduction:
(1) Randomaccessfile is a Java-provided access to the contents of the file class, can read files, can also write files.
(2) Support random access files, can access any location of the file
2. Open the file
There are two ways: "RW" (read-write) and "R" (read-only)
eg
Randomaccessfile RAF = new Randomaccessfile (file, rw);
3. File pointer
The file pointer here is the basis on which randomaccessfile can access any location in the file.
When opening a file, the pointer is at the beginning, pointer = 0;
method to get the file pointer: Raf.getfilepointer ();
4. Writing Method
Raf.write ();
You can write only one byte (that is, write only the last 8 digits), and the pointer will automatically point to the next position when you finish writing it, ready to write again.
5. Read the document
int i = Raf.read ();
Reads a byte from the position where the pointer is located, and converts the byte to the int type, which means that all of the bytes are populated to the last 8 bits of the Int.
Read (); only one byte can be read.
Read (byte[]); You can see inside a byte array
6. Close the stream
Raf.close ();
Be sure to close the stream when the file is read and written

The following is an example of the basic usage of the Randomaccessfile class:

Package com.test.random;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.RandomAccessFile;

Import Java.util.Arrays; public class Randomaccessfiletest {public static void main (string[] args) throws IOException {File dir = ne
        W File ("demo");//If no absolute path is written, the default is to create the file in the project if (!dir.exists ()) {dir.mkdir ();//Create the directory file}
        File File = new file (dir, "test.txt"); if (!file.exists ()) {file.createnewfile ();//Create file} randomaccessfile RAF = new Randomaccessfil E (file, rw);/set readable, writable operation System.out.println ("pointer position:" +raf.getfilepointer ());//pointer position/write a byte (the last 8 bits) go in (write in here
        The capacity is of type char, in fact, the char type is 8 bits (1 bytes), so it is possible to write a complete raf.write (' a ').
        System.out.println ("Position of the pointer:" +raf.getfilepointer ());//position of the pointer raf.write (' B ');
        System.out.println ("Already written length:" +raf.length ());//write length///write () a value of type int. (int is 4 bytes (32 bits) int i = 1; Raf.write (i>>>24&0xff)//write int of the highest 8 bits (8 bits between 24-32) Raf.write (I>>>16&0XFF),//write int (8 bits between 16-24) Raf.write (i>>> 8&0XFF)//write int (8 bits between 8-16) Raf.write (I&0XFF),//write int after 8 digits (8 bits between 0-8)//With Writeint () method (Randomaccessfil
        E with a method of writing int of the type), write an int of type value int j = 1;
        Raf.writeint (j); In fact, the Writeint () method internally is the value of the write int type implemented by the above method System.out.println ("pointer position:" +raf.getfilepointer ());

        System.out.println ("Length already written:" +raf.length ()); Write a string of values string s = "I";//Chinese is 2 bytes (16-bit) byte[] bytes = S.getbytes ();//convert String to byte array (byte type), byte Occupies 1 bytes (8 bit) raf.write (bytes);//write () method can write a byte array//If you want to read the contents of the file from scratch, you must move the pointer to the file head raf.seek (0);//Move the pointer to the
        File Header//one-time read, read the contents of the file to the byte array to byte[] buf = new byte[(int) raf.length ()];
        Raf.read (BUF);
        System.out.println (arrays.tostring (BUF));
        Encapsulated as String, string str = new string (BUF);
        System.out.println (str); Output for in 16-way(Byte b:buf)
        {System.out.println (integer.tohexstring (B&0XFF) + "");



    } raf.close ();
 }

}

Screenshot of results:

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.