Skip to content

Instantly share code, notes, and snippets.

@lqshow
Last active November 12, 2017 05:53
Show Gist options
  • Select an option

  • Save lqshow/ddfce7b5e71a213f5ed998ae945aa561 to your computer and use it in GitHub Desktop.

Select an option

Save lqshow/ddfce7b5e71a213f5ed998ae945aa561 to your computer and use it in GitHub Desktop.
Java NIO
package com.example.base.nio;
import java.io.IOException;
import java.nio.file.*;
public class FileExample {
public static void main(String[] args) {
String fileName = "/Users/linqiong/Downloads/profile";
Path path = Paths.get(fileName);
boolean pathExists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});
System.out.println(pathExists);
// Files.createDirectory()
Path newFolder = Paths.get("/Users/linqiong/Downloads/java");
try {
Path newDir = Files.createDirectory(newFolder);
} catch (FileAlreadyExistsException e) {
System.out.println("the directory already exists");
} catch (IOException e) {
e.printStackTrace();
}
// Files.copy()
Path sourcePath = Paths.get("/Users/linqiong/Downloads/profile");
Path destPath = Paths.get("/Users/linqiong/Downloads/java/profile");
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
} catch (FileAlreadyExistsException e) {
} catch (IOException e) {
e.printStackTrace();
}
// Files.delete()
try {
Files.delete(destPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Java NIO Path

Creating a Path Instance

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathExample {

    public static void main(String[] args) {
      	// Creating an Absolute Path (Linux, MacOS, FreeBSD etc.)
		String fileName = "/Users/linqiong/Downloads/profile";
        Path path = Paths.get(fileName);
      
      	// Creating a Relative Path
      	Path currentDir = Paths.get(".");
      	System.out.println(currentDir.toAbsolutePath());
      	// output: /Users/linqiong/workspace/app/java/base/.
          
        Path parentDir = Paths.get("..");
    	System.out.println(parentDir.toAbsolutePath());
    	// output: /Users/linqiong/workspace/app/java/base/..
    }
} 

Java NIO Files

Files.exists()

String fileName = "/Users/linqiong/Downloads/profile";
        Path path = Paths.get(fileName);
        boolean pathExists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

Files.createDirectory()

Path newFolder = Paths.get("/Users/linqiong/Downloads/java");
try {
	Path newDir = Files.createDirectory(newFolder);
} catch (FileAlreadyExistsException e) {
	System.out.println("The directory already exists");
} catch (IOException e) {
	e.printStackTrace();
}

Files.copy()

Path sourcePath = Paths.get("/Users/linqiong/Downloads/profile");
Path destPath = Paths.get("/Users/linqiong/Downloads/java/profile");
try {
	Files.copy(sourcePath, destPath);
} catch (FileAlreadyExistsException e) {
	//destination file already exists
} catch (IOException e) {
	e.printStackTrace();
}

Files.move()

Files.delete()

try {
	Files.delete(destPath);
} catch (IOException e) {
	e.printStackTrace();
}

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment