Character operation

Character operation

Encoding and decoding

Encoding is to convert characters into bytes, while decoding is to recombine bytes into characters.

If the encoding and encoding process use different encoding methods, there will be garbled code.

  • In GBK coding, Chinese characters occupy 2 bytes and English characters occupy 1 byte;
  • In UTF-8 coding, Chinese characters occupy 3 bytes and English characters occupy 1 byte;
  • In UTF-16be coding, both Chinese characters and English characters account for 2 bytes.

Be in UTF-16be refers to Big Endian, that is, big end. Correspondingly, there is also UTF-16le. Le refers to Little Endian, that is, small end.

Java's memory encoding uses double byte encoding UTF-16be. This does not mean that Java only supports this encoding method, but that char is encoded using UTF-16be. Char type takes up 16 bits, that is, two bytes. Java uses this double byte encoding so that a Chinese or an English can use a char to store.

Encoding method of String

String can be regarded as a character sequence. You can specify an encoding method to encode it into a byte sequence, or you can specify an encoding method to decode a byte sequence into string.

String str1 = "chinese";
byte[] bytes = str1.getBytes("UTF-8");
String str2 = new String(bytes, "UTF-8");
System.out.println(str2);

When calling the parameterless getBytes() method, the default encoding method is not UTF-16be. The advantage of double byte encoding is that a char can be used to store Chinese and English, which is no longer needed to convert String into bytes [] byte array, so double byte encoding is no longer needed. The default encoding method of GetBytes () is platform dependent, generally UTF-8.

byte[] bytes = str1.getBytes();
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "Hello";

        // String -- byte[]
        //byte[] bys = s.getBytes(); // [-28, -67, -96, -27, -91, -67]
        //byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
        //byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
        byte[] bys = s.getBytes("UTF-16be");//[79, 96, 89, 125]
        System.out.println(Arrays.toString(bys));

        // byte[] -- String
        //String ss = new String(bys); //  Hello
        //String ss = new String(bys, "GBK"); //  Hello
        //String ss = new String(bys, "UTF-8"); //  Hello
        String ss = new String(bys, "UTF-16be"); //Hello
        System.out.println(ss);
    }
}

Reader and Writer

Whether it is disk or network transmission, the smallest storage unit is bytes, not characters. However, data in the form of characters is usually operated in the program, so it is necessary to provide methods to operate characters.

  • InputStreamReader realizes decoding from byte stream into character stream;
  • OutputStreamWriter encodes character stream into byte stream.

InputStreamReader

  • Construction method of InputStreamReader:
    InputStreamReader(InputStream is) //Read data with default encoding
    InputStreamReader(InputStream is,String charsetName) //Read data with the specified encoding
public class ReaderDemo {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr=new InputStreamReader(
                new FileInputStream("demo1.txt"),"UTF-8");
        int ch=0;
        while((ch=isr.read())!=-1){
            System.out.print((char)ch);
        }

        isr.close();
    }
}
  • Read in character
    int read() //Read one character at a time
    int read(char[] chs) //Read one character array at a time
public class ReaderDemo2 {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr=new InputStreamReader(
                new FileInputStream("demo1.txt"),"UTF-8");

        // Read one character at a time
        // int ch = 0;
        // while ((ch = isr.read()) != -1) {
        // System.out.print((char) ch);
        // }

        // Read one character array at a time
        char[] chs = new char[1024];
        int len = 0;
        while ((len = isr.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }

        isr.close();
    }
}

OutputStreamWriter

  • Construction method of OutputStreamWriter:
    OutputStreamWriter(OutputStream out) //Convert the data of byte stream into character stream according to the default encoding
    OutputStreamWriter(OutputStream out,String charsetName) //Converts byte stream data into character stream according to the specified encoding
    //Character stream = byte stream + encoding table.
public class WriterDemo {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("demo1.txt"),"UTF-8");

        //Direct write character
        osw.write("Hello hello\r\n");

        osw.close();
    }
}
  • Write the method:
    public void write(int c) //Write a character
    public void write(char[] cbuf) //Write a character array
    public void write(char[] cbuf,int off,int len) //Write a part of a character array
    public void write(String str) //Write a string
    public void write(String str,int off,int len) //Write part of a string
public class WriterDemo2 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("demo1.txt"),"UTF-8");

        // Write data
        // public void write(int c): write a character
        osw.write('a');
        osw.write(97);
        // Why didn't the data go in?
        // The reason is: character = 2 bytes
        // The basic unit of data storage in a file is bytes.
        // void flush()

        // public void write(char[] cbuf): write a character array
        // char[] chs = {'a','b','c','d','e'};
        // osw.write(chs);

        // public void write(char[] cbuf,int off,int len): write a part of a character array
        // osw.write(chs,1,3);

        // public void write(String str): write a string
        // osw.write("I love Lin Qingxia");

        // public void write(String str,int off,int len): write a part of a string
        osw.write("I love Lin Qingxia", 2, 3);

        // refresh buffer 
        osw.flush();
        // osw.write("I love Lin Qingxia", 2, 3);

        // Release resources
        osw.close();
        // java.io.IOException: Stream closed
        // osw.write("I love Lin Qingxia", 2, 3);
    }
}

Note: what is the difference between close() and flush()?

  1. close() closes the stream object, but flushes the buffer first. After closing, the stream object can no longer be used.

  2. flush() only flushes the buffer. After flushing, the stream object can continue to be used.

BufferedReader

Read the text from the character input stream and buffer each character, so as to realize the efficient reading of characters, arrays and lines. You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.

public class ReaderDemo3 {
    public static void main(String[] args) throws IOException {
        // Create character buffered input stream object
        BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));

        // Mode 1
        // int ch = 0;
        // while ((ch = br.read()) != -1) {
        // System.out.print((char) ch);
        // }

        // Mode 2
        char[] chs = new char[1024];
        int len = 0;
        while ((len = br.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }

        // Release resources
        br.close();
    }
}

BufferedWriter

public class WriterDemo3 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

        bw.write("hello");
        bw.write("world");
        bw.write("java");
        bw.flush();

        bw.close();
    }
}

Special methods in character buffer stream

//BufferedWriter:
public void newLine() //Line breaks are determined according to the system
//BufferedReader:
public String readLine() //Read one row of data at a time
//The string containing the content of the line, without any line terminator. If the end of the stream has been reached, null will be returned
/**
 * Special method of character buffer stream:
 * BufferedWriter:
 *         public void newLine():Line breaks are determined according to the system
 * BufferedReader:
 *         public String readLine(): Read one row of data at a time
 *         The string containing the content of the line, without any line terminator. If the end of the stream has been reached, null will be returned
 */
public class BufferDemo {
    public static void main(String[] args) throws IOException {
        //write();
        read();
    }

    public static void write() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("demo1.txt"));

        for (int x = 0; x < 3; x++) {
            bw.write("I love Lin Qingxia");
            bw.newLine();
            bw.flush();
        }
        bw.write("Important things are to be repeated for 3 times");

        bw.close();
    }

    public static void read() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("demo1.txt"));

        String line=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }

        br.close();
    }
}

Character conversion stream

public class TransferDemo {
    public static void main(String[] args) throws IOException {
        // Encapsulating data sources
        BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));
        // Packaging destination
        BufferedWriter bw = new BufferedWriter(new FileWriter("demo4.txt"));

        // One of the two ways is to read and write one character array at a time
        char[] chs = new char[1024];
        int len = 0;
        while ((len = br.read(chs)) != -1) {
            bw.write(chs, 0, len);
            bw.flush();
        }

        // Release resources
        bw.close();
        br.close();
    }
}
public class TransferDemo2 {
    public static void main(String[] args) throws IOException {
        // Encapsulating data sources
        BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));
        // Packaging destination
        BufferedWriter bw = new BufferedWriter(new FileWriter("demo4.txt"));

        String line=null;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        // Release resources
        bw.close();
        br.close();
    }
}

Copy text file

public class CopyFile {
    public static void main(String[] args) throws IOException{
        String srcFile="pride-and-prejudice.txt";
        String destFile="demo5.txt";
        long start=System.currentTimeMillis();
        //method(srcFile,destFile);// It took 191 milliseconds
        //method2(srcFile,destFile);// It took 59 milliseconds
        //method3(srcFile,destFile);// It took 68 milliseconds
        //method4(srcFile,destFile);// It took 38 milliseconds
        method5(srcFile,destFile);//It took 177 milliseconds
        long end=System.currentTimeMillis();
        System.out.println("Total time"+(end-start)+"millisecond");
    }

    //The basic character stream reads and writes one character at a time
    public static void method(String srcFile,String destFile) throws IOException{
        FileReader fr=new FileReader(srcFile);
        FileWriter fw=new FileWriter(destFile);

        int ch=0;
        while((ch=fr.read())!=-1){
            fw.write(ch);
        }

        fr.close();
        fw.close();
    }

    //The basic character stream reads and writes one character array at a time
    public static void method2(String srcFile,String destFile) throws IOException{
        FileReader fr=new FileReader(srcFile);
        FileWriter fw=new FileWriter(destFile);

        int len=0;
        char[] chs=new char[1024];
        while((len=fr.read(chs))!=-1){
            fw.write(chs,0,len);
        }

        fr.close();
        fw.close();
    }

    //The character buffer stream reads and writes one character at a time
    public static void method3(String srcFile,String destFile) throws IOException{
        BufferedReader br=new BufferedReader(new FileReader(srcFile));
        BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));

        int ch=0;
        while((ch=br.read())!=-1){
            bw.write(ch);
        }

        br.close();
        bw.close();
    }

    //The character buffer stream reads and writes one character array at a time
    public static void method4(String srcFile,String destFile) throws IOException{
        BufferedReader br=new BufferedReader(new FileReader(srcFile));
        BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));

        int len=0;
        char[] chs=new char[1024];
        while((len=br.read(chs))!=-1){
            bw.write(chs,0,len);
        }

        br.close();
        bw.close();
    }

    public static void method5(String srcFile,String destFile) throws IOException{
        BufferedReader br=new BufferedReader(new FileReader(srcFile));
        BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));

        String line=null;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        br.close();
        bw.close();
    }
}

Copy multi-level folders

public class CopyFolders {
    public static void main(String[] args) throws IOException {
        File srcFile=new File("demo2");
        File destFile=new File("demo3");
        copyFolder(srcFile,destFile);
    }

    //Copy multi-level folders
    public static void copyFolder(File srcFile,File destFile) throws IOException {
        if(srcFile.isDirectory()){
            //Create a new folder under destFile
            File newDirectory=new File(destFile,srcFile.getName());
            newDirectory.mkdir();

            // Get all files or folders under the File object
            File[] files=srcFile.listFiles();
            if(files!=null){
                //Copy all files under srcFile to the new destFile
                for(File file:files){
                    copyFolder(file,newDirectory);
                }
            }
        }else{
            File newFile=new File(destFile,srcFile.getName());
            copyFile(srcFile,newFile);
        }
    }

    //Copy file
    public static void copyFile(File srcFile,File destFile) throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

        int len=0;
        byte[] bys=new byte[1024];
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }

        bos.close();
        bis.close();
    }
}

Keywords: Java Back-end

Added by davemwohio on Thu, 24 Feb 2022 13:16:26 +0200