基本的创建文件
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
public class TestCreateFile {
public static void main(String[] args) {
//先创建一个文件夹
File scFileFolder = new File("e:/SCfile");
if(scFileFolder.exists()){
System.out.println("确定路径存在 文件夹创建成功"+scFileFolder.getAbsolutePath());
}
//然后在文件下面创建一个txt文件(空的里面啥都没有)
try {
File file = new File(scFileFolder,"maru.txt");
if (file.getParentFile().exists()) {
if (file.exists()) {
System.out.println("文件已经存在");
} else {
file.createNewFile();
}
}
}catch (IOException e){
e.printStackTrace();
}
//另一种方式 Path 和Files 是java7里面引入的
Path filepath = Paths.get("e:/SCfile","seral.txt");
try {
if (Files.exists(filepath)){
System.out.println("文件已经存在");
}else {
Files.createFile(filepath);
}
}catch (IOException e){
e.printStackTrace();
}
}
}