import java.io.File; import java.io.IOException; public class gh { public static void main(String[] args) { File file=new File("../hello.txt"); //在上一级文件夹创建文件 File file2=new File("../../hello.txt"); //在上上一级文件夹创建文件 if(file.exists()) //判断文件是否存在,返回值是boolean file.isFile(); //判断是不是一个文件,返回值为boolean file.isDirectory(); //判断是不是一个文件夹,返回值为boolean try { file.createNewFile(); System.out.println("文件创建成功"); //创建文件 } catch (IOException e) {e.printStackTrace();} file.delete(); //删除文件,不用管地址在哪里,直接删除 // File nameto =new File("new file") ; //重命名 File nameto =new File("src/new file") ; //重命名并移动位置 file.renameTo(nameto); //将file重命名为nameto的名字,并且移动到相应路径 System.out.println(file.getName());//得到文件名称 System.out.println(file.getPath());//得到文件路径 System.out.println(file.getParentFile());//得到父级路径 file.getParentFile();//得到父级文件,返回值是file System.out.println(file.getAbsolutePath());//得到绝对路径 file.length(); //得到文件大小 file.isHidden(); //判断文件是否隐藏 file.canRead(); //判断文件是否可读 file.canWrite(); //判断文件是否可写 file.setWritable(true);//文件设置可写 file.setReadable(true);