This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The problem

  • When reading and writing Chinese files in Java, garbled characters often appear in the content read or written

why

  • The coding of the system and the coding of the program adopt different coding formats
    • The coding format used by Windows itself is GBK. GBK and GB2312 are basically the same coding method
    • Encode in IDEA is the UTF-8 encoding by default
    • When a file is created and written in Windows with the GBK encoding, it is read in UTF-8 mode by a program directly, which is garbled
  • To avoid possible Chinese garble problems, it is best to specify the encoding format explicitly when the file is written and read

To solve

  • inJavaIn the usejava.io.FileReaderorjava.io.FileWriterTo read and write files
    • Although efficiency can be improved with java.io.BufferedReader and java.io.BufferedWriter
    • However, in FileReader and FileWriter, only the encoding mode can be obtained, not set
    • This results in the encodings in FileReader and FileWriter being subject to the underlying encodings, resulting in garbled characters when reading or writing files encoded in multiple languages
  • Using Java. IO. FileInputStream or Java. IO. InputStreamReader and Java. IO. FileOutputStream or Java. IO. OutputStreamWriter to solve this problem
  • In InputStreamReader and OutputStreamWriter, GBK files can be read and written by specifying the encoding mode
public class ReadAndWrite {  
    private static void test(a){  
        File firstFile = new File("D://fileone.txt");  
        File secondFile=new File("D://filesecond.txt");  
        BufferedReader in = null;  
        BufferedWriter out = null;        
        try {       
            // Add to the coded character set
            in = new BufferedReader(new InputStreamReader(new FileInputStream(firstFile), "gbk"));  
            // Add to the coded character set
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(secondFile), "gbk"));  
            String line = "";  
            while((line = in.readLine())! =null){  
                System.out.println(line);  
                out.write(line+"\r\n"); }}catch (FileNotFoundException e) {  
            System.out.println("file is not fond");  
        } catch (IOException e) {  
            System.out.println("Read or write Exceptioned");  
        }finally{             
            if(null! =in){try {  
                    in.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }}  
            if(null! =out){try {  
                    out.close();  
                } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

conclusion

  • BufferedWriter must be turned off at last, otherwise the content will not be written to the file
  • When using the readLine() method, a newline is written to add