java.nio.file.Files 将 String 作为字节读取或写入的方法调用
此类调用可被替换为对 Java 11 中引入的 Files.readString() 或 Files.writeString() 方法的调用。
示例:
String s = "example";
Files.write(Paths.get("out.txt"), s.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE);
s = new String(Files.readAllBytes(Paths.get("in.txt")), StandardCharsets.ISO_8859_1);
在应用快速修复后:
String s = "example";
Files.writeString(Paths.get("out.txt"), s, StandardOpenOption.WRITE);
s = Files.readString(Paths.get("in.txt"), StandardCharsets.ISO_8859_1);
请注意,当涉及到处理无效(不可映射)字符时,readString() 行为不同于 new String(bytes, charset) 行为。 在这种情况下,readString() 方法会抛出异常,而 new String(bytes, charset) 方法会以静默方式将无效字符替换为替换字符。
如果希望进行静默替换,则最好禁止检查警告。
2018.3 最新变化