[Java] Corrective notes for "Invalid escape character" and "Cannot map to encoding MS932"

Good evening.

I'm currently working on a big project for myself. (Because it may fail, I will hide the contents ... If I can publish it here someday!)

In order to make a small part of the program, I am making a program that can read and output text in Java. Since it became windows10, the default character code has become UTF-8. I tried to set the input / output of UTF-8 file with Vbscript, but the characters are garbled if it is the conventional process dedicated to Shift-Jis. Do Vbscript users feel like they have changed the coding method? Example) Processing to read a record in a text file dataline = inStream.readline       ↓ dataline = inStream.readtext(-2) I have a lot of Vbscript tools stored on my company's PC, so I'm wondering how to deal with it. Well, because of that, I decided to work with Java while I was studying. I have dealt with the compilation error that occurred there, so I will post it as a memo.

Conclusion first

Two errors have occurred. Each action is described by →.

Error: This character cannot be mapped to encoding MS932 → ① Coded by specifying "-encoding UTF-8" at compile time. Compile to the encoding of the source file → ② When saving the source file, the default encoding (in this case, MS932) This is an extended version of Shift-Jis, so save it with'ANSI') -Error: The escape character is invalid → For the character string to be escaped, use an escape sequence, etc. Corresponding ('' →'\' etc.)

Program made

Well, I made the following program. The process is very simple, it is a process to read a text file and output it to the screen. (I borrowed a sample program from a certain site)

readtest.java


import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class readtest{
public static void main(String args[]){
try{
//err1  File file = new File("C:\Users\document\Java1\sinki\K0040d.txt");
//err2  File file = new File("C:\\Users\\document\\Java1\\sinki\\K00401.txt");
  File file = new File("K0040d.txt");
  BufferedReader br = new BufferedReader(new FileReader(file));

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

    str = br.readLine();
  }

  br.close();
}catch(FileNotFoundException e){
  System.out.println(e);
}catch(IOException e){
  System.out.println(e);
}

}
}

First, it is'err1'.

\Users\document\Java1\sinki\K0040d.txt");


 If you enable this and compile, the following error will be output.

readtest.java:13:error:This character cannot be mapped to encoding MS932 // File file = new File("C:\Users\Replying to @Mr.?\Java1\sinki\K0040d.txt"); ^ readtest.java:10:error:Illegal escape character File file = new File("C:\Users\Replying to @Mr.?\Java1\sinki\K0040d.txt"); ^


 The reason is simple: you have to escape the'\' when you specify the path. So, change from'\' to'\\\'.

 It is err2 that corresponds to this.

#### **`\\Users\\document\\Java1\\sinki\\K00401.txt");`**
```//err2  file file = new file("c


 (By the way, in the end, I stopped specifying the absolute path and only used the file name.
 The text file I wanted to read was stored in the same folder, so this was fine)

 After that, when compiling, it seems that only the escape character error was resolved, but the following error remains.

readtest.java:13:error:This character cannot be mapped to encoding MS932 // File file = new File("C:\Users\Replying to @Mr.?\Java1\sinki\K0040d.txt"); ^

 First, you need to be aware of the default encoding for your environment. If you handle the source file with a different character code, the above error will occur.

 When I checked the default encoding with the program described on the following site, it was MS932.
https://www.javadrive.jp/start/encoding/index2.html

 On the other hand, the Java program file created this time is used when saving the text file.
 "UTF-8" is selected.
 In that case, the character code of MS932 cannot be read and an error occurs.

 There are two countermeasures.
 -Save as "ANSI" when saving the file.
 -When compiling, specify the following and compile.
 ```javac -encoding UTF-8 readtest.java```
 Character code used for xxx of "-encoding xxx"
 Specify (character code you want to encode).
 
 When I ran it with this, the contents of the text file came out! Wow wow.
`2020/03/20      xxx     -100
 2020/04/20      xxx     -100
 2020/05/18      xxx     -100
 2020/06/20      xxx     -100
 2020/07/20      xxx     -100
 2020/08/20      xxx     -100
 2020/09/20      xxx     -100`

 (The contents of the text file have no meaning)

 So that's it for today when I was able to read the text file and output it to the screen.
 I would like to output steadily like this.

 see you.


Recommended Posts

[Java] Corrective notes for "Invalid escape character" and "Cannot map to encoding MS932"
Javadoc generation error: This character cannot be mapped to encoding MS932
Minecraft Forge error: This character cannot be mapped to encoding MS932
Review notes for Java 1.7 and later file copies
Java compilation error Unable to map to encoding windows-31j
Java upload and download notes to Azure storage
Reasons to avoid for and map, filter, reduce, etc.
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
How to loop Java Map (for Each / extended for statement)
Store in Java 2D map and turn with for statement
How to write and notes when migrating from VB to JAVA
For Java beginners: List, Map, Iterator / Array ... How to convert?
Notes on character encoding when migrating from windows to Mac