ADDITIONAL SYSTEM INFORMATION :
macOS 14.3.1
Java version: OpenJDK 22.0 macOS / AArch64
A DESCRIPTION OF THE PROBLEM :
Strings encoded in Shift_JIS become garbled when read from files.
It worked fine until Java 21 as follows:
Java Version:21.0.2
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:SJIS
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
REGRESSION : Last worked in version 21.0.2
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the attached source code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Java Version:22
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:Shift_JIS
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
ACTUAL -
Java Version:22
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:Shift_JIS
日本語です
Decoded string matches:true
åe,gg0Y0
Content from file matches:false
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Java Version:" + System.getProperty("java.version"));
String[] encodings = {"UTF-8", "Shift_JIS"};
for (String encoding : encodings) {
test(encoding);
}
}
static void test(String encoding) throws IOException {
System.out.println("---------Testing:" + encoding);
Charset charset = Charset.forName(encoding);
Path path = Path.of(encoding + "_encoded.txt");
String originalContent = "日本語です";
byte[] encodedBytes = originalContent.getBytes(charset);
String decodedString = new String(encodedBytes, charset);
System.out.println(decodedString);
System.out.println("Decoded string matches:" + decodedString.equals(originalContent));
Files.writeString(path, originalContent, charset);
String read = Files.readString(path, charset);
System.out.println(read);
boolean matches = read.equals(originalContent);
System.out.println("Content from file matches:" + matches);
}
}
---------- END SOURCE ----------
FREQUENCY : always