java 7中创建文件和目录

在java 7中,可以象下面那样创建文件和目录,用新的api咯

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

public class FileCreation {
	public static void main(String[] args) {
		Path pathToDirectory = FileSystems.getDefault().getPath(
				"C:/Ablage/dev/workspace");
		try {
			Files.createDirectories(pathToDirectory);
			System.out
					.println("Directory created at path : " + pathToDirectory);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Path pathToFile = FileSystems.getDefault().getPath(
				"C:/Ablage/dev/workspace/hello.pdf");
		try {
			Files.createFile(pathToFile);
			System.out.println("File created at path : " + pathToFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(java)