English 中文(简体)
Java 11 - File APIs
  • 时间:2024-12-22

Java 11 - File APIs


Previous Page Next Page  

Java 11 introduced an easy way to read and write files by providing new overloaded methods without writing much boiler plate code.

Consider the following example −

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;

pubpc class APITester {
   pubpc static void main(String[] args) {		
      try {
         Path tempFilePath = Files.writeString(
            Path.of(File.createTempFile("tempFile", ".tmp").toURI()),
            "Welcome to TutorialsPoint", 
            Charset.defaultCharset(), StandardOpenOption.WRITE);

         String fileContent = Files.readString(tempFilePath);

         System.out.println(fileContent);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output


Welcome to TutorialsPoint
Advertisements