①public File(String pathname)
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果是相对路径,则默认相对于当前project。
File file1 = new File(“hello.txt”); //相对路径
File file2 = new File(“C:\IDEA\untitled\file\hi.txt”); //绝对路径
②public File(String parent,String child)
以parent为父路径,child为子路径创建File对象。
File file3 = new File(“C:\IDEA\untitled\”, “file”);
③public File(File parent,String child)
根据一个父File对象和子文件路径创建File对象
File file3 = new File(“C:\IDEA\untitled\”, “file”);
File file4 = new File(file3, “hi.txt”);
①File类的获取功能
public String getAbsolutePath()
:获取绝对路径
public String getPath()
:获取路径
public String getName()
:获取名称
public String getParent()
:获取上层文件目录路径,若无,返回null
public long length()
:获取文件长度(即:字节数),不能获取目录的长度
public long lastModified()
:获取最后一次的修改时间,毫秒值
public String[] list()
:获取指定目录下的所有文件或者文件目录的名称数组
public File[] listFiles()
:获取指定目录下的所有文件或文件目录构成的数组
import java.io.File;
/**
-
@Author: Yeman
-
@Date: 2021-09-24-21:50
-
@Description:
*/
public class FileTest {
public static void main(String[] args) {
File file1 = new File(“hello.txt”);
File file2 = new File(“C:\IDEA\untitled\file\hi.txt”);
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(file1.lastModified());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
}
②File类的重命名功能
public boolean renameTo(File dest)
:把文件重命名为指定的文件路径和文件名,相当于是把真实文件转移并且重命名了
import java.io.File;
/**
-
@Author: Yeman
-
@Date: 2021-09-24-21:50
-
@Description:
*/
public class FileTest {
public static void main(String[] args) {
File file1 = new File(“hello.txt”); //file1需要在硬盘中真实存在
File file2 = new File(“C:\IDEA\hi.txt”); //在硬盘中不存在file2
boolean b = file1.renameTo(file2);
System.out.println(b);
最后
整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
其实面试这一块早在第一个说的25大面试专题就全都有的。以上提及的这些全部的面试+学习的各种笔记资料,我这差不多来回搞了三个多月,收集整理真的很不容易,其中还有很多自己的一些知识总结。正是因为很麻烦,所以对以上这些学习复习资料感兴趣,
…(img-lZPVOFd8-1714324535225)]
[外链图片转存中…(img-OiXBMZE4-1714324535226)]
其实面试这一块早在第一个说的25大面试专题就全都有的。以上提及的这些全部的面试+学习的各种笔记资料,我这差不多来回搞了三个多月,收集整理真的很不容易,其中还有很多自己的一些知识总结。正是因为很麻烦,所以对以上这些学习复习资料感兴趣,