[Java] File system operation

File system operation

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
  public static void main(String[] args) {
    try {
      //Get current file system
      var fs = FileSystems.getDefault();
      var path1 = fs.getPath("./sample.txt");
      //File existence confirmation
      System.out.println(Files.exists(path1));       //true
      //Is the file readable?
      System.out.println(Files.isReadable(path1));   //true
      //Is the file writable?
      System.out.println(Files.isWritable(path1));   //true
      //Is the file executable?
      System.out.println(Files.isExecutable(path1)); //true
      //file size
      System.out.println(Files.size(path1));
      //File copy
      var path2 = Files.copy(path1, fs.getPath("./copy.txt"),
        StandardCopyOption.REPLACE_EXISTING);
      //Move file
      Files.move(path2, fs.getPath("./sub/copy.txt"),
        StandardCopyOption.REPLACE_EXISTING);
     //File name change
      var path3 = Files.move(path1, fs.getPath("./sub/rename.txt"),
        StandardCopyOption.REPLACE_EXISTING);
      //File deletion
      Files.delete(path3);
      //Delete if the file exists (no exception occurs)
      Files.deleteIfExists(path3);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Folder operation

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
  public static void main(String[] args) {
    try {
      var fs = FileSystems.getDefault();
      var dir1 = fs.getPath("./playground");
      var dir2 = fs.getPath("./data");
      //Create folder
      Files.createDirectories(dir1);
      //Does the folder exist?
      System.out.println(Files.exists(dir1)); //true
      //Is it a folder?
      System.out.println(Files.isDirectory(dir1)); //true
      //Subfile under dir2/Get folder stream
      var s = Files.list(dir2);
      //.Get filename ending in log
      s.filter(v -> v.getFileName().toString().endsWith(".log")).
        forEach(System.out::println); //./data/test.log
      //Folder copy
      var dir3 = Files.copy(dir1, fs.getPath("./data"),
        StandardCopyOption.REPLACE_EXISTING);
      //Move folder
      Files.move(dir3, fs.getPath("./data"),
          StandardCopyOption.REPLACE_EXISTING);
      //Delete folder
      Files.delete(fs.getPath("./data/sub"));
      //Delete only when the folder exists
      Files.deleteIfExists(fs.getPath("./data/sub"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Recommended Posts

[Java] File system operation
java file creation
Read Java Property file
java (split source file)
java core: chopped core file
Java8 Stream reduction operation
Basics of character operation (java)
[Java] Create a temporary file
[Java] [Android] Read ini file
[Java] Stream API intermediate operation
Operate Azure Blob Storage in Java (SAS token, file operation)
Operation to connect multiple Streams @Java
Read Java properties file in C #
Upload a file using Java HttpURLConnection
Run a batch file from Java
Unzip the zip file in Java
Log output to file in Java
Java
EXCEL file update sample with JAVA
About file copy processing in Java
Java
[Java] How to use the File class
Re-study Docker from a system operation perspective
View file system information for mounted volumes
What is the best file reading (Java)
Why does Java call a file a class?
[Java] Collection and StringBuilder operation method comparison
Read xlsx file in Java with Selenium
Java (exception handling, threading, collection, file IO)
Java: Place the ResourceBundle properties file anywhere
Sample to unzip gz file in Java
[Java8] Search the directory and get the file
Element operation method in appium TIPS (Java)
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java