File的常用方法有哪些?
1、常见构造方法
(1)public File(String pathname) ;//绝对路径构造
(2)public File(String parent, String child);//相对路径构造
(3)public File(File parent, String child);//若不存在parent实例,先创建。再相对路径构造。
public class demo02File {
public static void main(String[] args) {
String fatherPath = "D:/other";
String name = "1.jpg";
// 相对路径构造
File src1 = new File(fatherPath, name);
File src2 = new File(new File(fatherPath), "2.jpg");
// 绝对路径构造
File src3=new File("D:/other/3.jpg");
//没有盘符:以user.dir构建
File src4= new File("4.jpg");
System.out.println(src4.getName());
System.out.println(src4.getPath());
System.out.println(src4.getAbsolutePath());
//.表示当前路径
File src5= new File(".");
System.out.println(src5.getName());
System.out.println(src5.getPath());
System.out.println(src5.getAbsolutePath());
}
}
上述代码运行结果:
4.jpg
4.jpg
D:\mywork\linlinjava\javalearnig\JavaStudy\4.jpg
.
.
D:\mywork\linlinjava\javalearnig\JavaStudy.
2、获取信息方法
getPath() :得到file的路径
getName() :得到最后一层的名字,即文件名
getParent() :得到去掉最后一层的路径,即文件的上层目录
getParentFile() :得到父类路径的新文件
String fatherPath = "D:/other";
String name = "1.jpg";
File src1 = new File(fatherPath, name);
System.out.println("文件路径:" + src1.getPath());
System.out.println("文件名:" + src1.getName());
System.out.println("文件上层目录:" + src1.getParent());
File file1 = src1.getParentFile();// 得到父类路径文件的新对象
//String[] filesName = file1.list();// 返回路径下文件名的String数组
//File[] files = file1.listFiles();//获取路径下文件组成的File数组
3、判断方法
isDirectory() 是否为文件夹
isFile() 是否为文件
exists() 路径是否存在
canWrite()是否可写
canRead()是否可读
length()文件长度?可以用来判断是否是文件,文件夹长度为0
String fatherPath = "D:/other";
String name = "1.jpg";
File src1 = new File(fatherPath);
File src2 = new File(fatherPath, name);
System.out.println("是否存在? "+src1.exists());
System.out.println("是否是文件夹? "+src1.isDirectory());
System.out.println("是否是文件? "+src2.isFile());
System.out.println("文件是否可写? " + src2.canWrite());
System.out.println("文件是否可读? " + src2.canRead());
System.out.println("是否是绝对路径? " + src1.isAbsolute());
System.out.println("文件长度? "+src1.length());// 文件的长度(字节数),文件夹长度为0,只有文件可以读取长度
4、创建/删除文件
createNewFile();创建文件
delete();删除文件
createTempFile();创建临时文件
tempFile.deleteOnExit();临时文件在程序退出后自动删除
public static void test3() throws IOException, InterruptedException {
String path = "D:/other/0.jpg";
File f1 = new File(path);
if (!f1.exists()) {
boolean flag = f1.createNewFile();
System.out.println(flag ? "创建成功" : "创建失败");
} else {
System.out.println("文件已存在!");
}
boolean flag = f1.delete();
System.out.println(flag ? "删除成功" : "删除失败");
//临时文件.
File tempFile=File.createTempFile("tes", ".temp", new File("D:/other"));
Thread.sleep(2000);
tempFile.deleteOnExit();//程序退出后自动删除
}
5、操作目录方法
mkdir() 创建新文件夹,只能创建一层
mkdirs() 创建新文件夹,可以多层
list() :返回该路径下文件或者文件夹的名字数组
listFiles() :返回该路径下文件或者文件夹组成的File数组
/**
*代码功能:输出文件夹下的文件名称。
*输出文件夹下的文件路径。
*输出文件夹中的txt文件路径
**/
public class demo04File {
public static void main(String[] args) {
String path = "D:/other/test/mytest";
File f1 = new File(path);
f1.mkdir();// 创建目录,必须确保父目录存在,否则失败
f1.mkdirs();// 创建目录,如果父目录不存在,一同创建
if (f1.exists()) {
System.out.println("=====子目录||子文件====");
String[] names = f1.list();
for (String temp : names) {
System.out.println(temp);
}
System.out.println("=====子目录||子文件的file对象====");
File[] files=f1.listFiles();
for(File temp:files){
System.out.println(temp.getAbsolutePath());
}
System.out.println("=====子目录||文件txt对象====");
//命令设计模式
File[] files1=f1.listFiles(new FilenameFilter() {
/**
* dir代表f1
*/
@Override
public boolean accept(File dir, String name) {
return new File(dir,name).isFile()&& name.endsWith(".txt");
}
});
for(File temp:files1){
System.out.println(temp.getAbsolutePath());
}
}
}
}
以上代码运行结果:
=子目录||子文件
1.docx
2.mpp
3.xlsx
4.txt
=子目录||子文件的file对象
D:\other\test\mytest\1.docx
D:\other\test\mytest\2.mpp
D:\other\test\mytest\3.xlsx
D:\other\test\mytest\4.txt
=子目录||文件txt对象
D:\other\test\mytest\4.txt