Java 11 文件APIs
Java 11通过提供新的重载方法,无需编写繁琐的样板代码,提供了一种方便读写文件的方式。
考虑以下示例 –
ApiTester.java
import java.io.File;import java.io.IOException;import java.nio.charset.Charset;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardOpenOption;public class APITester { public static void main(String[] args) { try { Path tempFilePath = Files.writeString( Path.of(File.createTempFile("tempFile", ".tmp").toURI()), "Welcome to Geek-docs.com", Charset.defaultCharset(), StandardOpenOption.WRITE); String fileContent = Files.readString(tempFilePath); System.out.println(fileContent); } catch (IOException e) { e.printStackTrace(); } }}输出
Welcome to Geek-docs.com
