package io;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
File f=new File("C:/create");//如果没有创建一个文件夹
File f2=new File("C:/create.txt");//创建一个文本
if(!f.isFile()){
f.mkdirs();//父目录如果没有也一并创建
}
else{
System.out.println("文件夹create已经存在");
}
if(!f2.isFile()){
try {
f2.createNewFile();//当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
} catch (IOException e) {
e.printStackTrace();
}
}
else{
System.out.println("文件create.txt已经存在");
}
try {
File ff=new File("c:/rename");
f.renameTo(ff);//重命名文件夹名字
System.out.println(f.getAbsolutePath());//返回此抽象路径名的绝对路径名字符串
System.out.println(f.getCanonicalPath());//返回此抽象路径名的规范路径名字符串
System.out.println(f.getName());//获取文件名
System.out.println(f.getPath());//将此抽象路径名转换为一个路径名字符串
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
File f=new File("C:/create");//如果没有创建一个文件夹
File f2=new File("C:/create.txt");//创建一个文本
if(!f.isFile()){
f.mkdirs();//父目录如果没有也一并创建
}
else{
System.out.println("文件夹create已经存在");
}
if(!f2.isFile()){
try {
f2.createNewFile();//当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
} catch (IOException e) {
e.printStackTrace();
}
}
else{
System.out.println("文件create.txt已经存在");
}
try {
File ff=new File("c:/rename");
f.renameTo(ff);//重命名文件夹名字
System.out.println(f.getAbsolutePath());//返回此抽象路径名的绝对路径名字符串
System.out.println(f.getCanonicalPath());//返回此抽象路径名的规范路径名字符串
System.out.println(f.getName());//获取文件名
System.out.println(f.getPath());//将此抽象路径名转换为一个路径名字符串
} catch (IOException e) {
e.printStackTrace();
}
}
}