File类就是将文件或者文件夹封装成一个对象。
格式特点:
- File(String pathname)
可以把一个存在或者不存在的文件(文件夹)封装成一个对象 - File(String Parent,String Child)
- File(File Parent,String Child)
package chapter12;
import java.io.File;
public class FileDemo1 {
public static void main(String[] args)
{
}
public void FileTest()
{
//文件路径格式: /一个斜杠 或者 \\两个反斜杠
File f1=new File("E:/practice/a.txt");
File f2=new File("E:/practice","a.txt");
File parent=new File("E:/practice");
File f3=new File(parent,"a.txt");
//合并上两步
File f4=new File(new File("E:/practice"),"a.txt"); //类似匿名对象
}
}
创建文件
- createNewFile() 创建文件
如果文件不存在,就创建文件并返回true,否则就返回false
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo1 {
public static void main(String[] args) throws IOException
{
FileDemo1 fd=new FileDemo1();
fd.FileTest();
}
public void FileTest() throws IOException
{
//文件路径格式: /一个斜杠 或者 \\两个反斜杠
File f=new File("E:/practice.txt");
File f1=new File("E:/practice");
File f2=new File("E:/practice","a.txt");
File parent=new File("E:/practice");
File f3=new File(parent,"a.txt");
//合并上两步
File f4=new File(new File("E:/practice"),"a.txt"); //类似匿名对象
//创建文件
boolean flag=f.createNewFile();
boolean flag1=f1.createNewFile();
System.out.println(flag);
System.out.println(flag1);
}
}
创建文件夹
- mkdir() 创建单级文件夹
- mkdirs() 创建多级文件夹
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo1 {
public static void main(String[] args) throws IOException
{
FileDemo1 fd=new FileDemo1();
fd.FileTest();
}
public void FileTest() throws IOException
{
//文件路径格式: /一个斜杠 或者 \\两个反斜杠
File f=new File("E:/practice");
File f1=new File("E:/p/a/b/c/d");
File f2=new File("E:/practice","a.txt");
File parent=new File("E:/practice");
File f3=new File(parent,"a.txt");
//合并上两步
File f4=new File(new File("E:/practice"),"a.txt"); //类似匿名对象
/*创建文件
boolean flag=f.createNewFile();
boolean flag1=f1.createNewFile();
System.out.println(flag);
System.out.println(flag1);*/
//创建文件夹
boolean flag=f.mkdir();
System.out.println(flag);
boolean flag1=f1.mkdirs();
System.out.println(flag1);
}
}
删除文件
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo1 {
public static void main(String[] args) throws IOException
{
FileDemo1 fd=new FileDemo1();
fd.FileTest();
}
public void FileTest() throws IOException
{
//文件路径格式: /一个斜杠 或者 \\两个反斜杠
File f=new File("E:/practice");
File f1=new File("E:/p");
File f2=new File("E:/practice","a.txt");
File parent=new File("E:/practice");
File f3=new File(parent,"a.txt");
//合并上两步
File f4=new File(new File("E:/practice"),"a.txt"); //类似匿名对象
/*创建文件
boolean flag=f.createNewFile();
boolean flag1=f1.createNewFile();
System.out.println(flag);
System.out.println(flag1);*/
//创建文件夹
f.mkdir();
f1.createNewFile();
//删除
boolean flag=f.delete();
boolean flag1=f1.delete();
System.out.println(flag);
System.out.println(flag1);
}
}
判断
- exists() 判断文件或者文件夹是否存在
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException
{
new FileDemo2().TestPanDuan();
}
public void TestPanDuan() throws IOException
{
File f1=new File("E:\\Practice");
f1.createNewFile();
File f2=new File("E:\\p\\a\\b\\c\\d");
f2.mkdirs();
boolean flag1=f1.exists(); //判断文件是否存在
System.out.println(flag1);
}
}
- isDirectory() 判断文件是否为文件夹
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException
{
new FileDemo2().TestPanDuan();
}
public void TestPanDuan() throws IOException
{
File f1=new File("E:\\Practice");
f1.createNewFile();
File f2=new File("E:\\p\\a\\b\\c\\d");
f2.mkdirs();
/*boolean flag1=f1.exists(); //判断文件是否存在
System.out.println(flag1);*/
boolean flag2=f2.isDirectory(); //判断对象是不是文件夹
boolean flag3=f1.isDirectory();
System.out.println(flag2);
System.out.println(flag3);
}
}
- isFile() 判断对象是否为文件
package chapter12;
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException
{
new FileDemo2().TestPanDuan();
}
public void TestPanDuan() throws IOException
{
File f1=new File("E:\\Practice");
f1.createNewFile();
File f2=new File("E:\\p\\a\\b\\c\\d");
f2.mkdirs();
boolean flag2=f2.isFile(); //判断对象是否为件夹
boolean flag3=f1.isFile();
System.out.println(flag2);
System.out.println(flag3);
}
}
- isAbsolute() 判断文件是否是绝对路径
- isHidden() 判断文件是否隐藏
获取
常用方法
- getAbsoluteFile() 获取绝对路径并返回File类型
- getAbsolutePath() 获取绝对路径返回String类型
- getParentFile() 获取当前路径的父路径,并以File形式返回
- getParent() 获取当前路径的父路径,并返回String
- getPath() 获取当前路径
- getName() 获取当前文件或文件夹的名称
- lastModified() 获取最后修改时间
- getTotalSpace() 获取分区总大小
- length() 获取文件长度