Java 11 文件APIs

来源:这里教程网 时间:2026-02-17 21:22:17 作者:

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

相关推荐