File类的构造方法以及获取功能方法
File类的构造方法
File(File parent, String child)
从父抽象路径名和子路径名字符串创建新的
File 实例。 |
File(String pathname)
通过将给定的路径名字符串转换为抽象路径名来创建新的
File 实例。 |
File(String parent, String child)
从父路径名字符串和子路径名字符串创建新的
File 实例。 |
File(URI uri)
通过将给定的 file: URI转换为抽象路径名来创建新的 File实例。
|
public class demg_02 { public static void main(String[] args) { File file = new File("E:\\s.txt"); System.out.println(file); show01(); show02("E:\\","s.txt"); }
//File(String parent, String child):
从父路径名字符串和子路径名字符串创建新的File
实例。好处:父路径和子路径,可以单独书写,使用起来非常灵活,父路径和子路径都可以变化
private static void show02(String f,String m) { File file = new File(f, m); System.out.println(file); }
//File(String pathname):
通过将给定的路径名字符串转换为抽象路径名来创建新的File
实例。
private static void show01() { File file = new File("E:\\s.txt"); System.out.println(file); } }
File类获取功能的方法
String getAbsolutePath():返回此File的绝对路径名字字符串 String getPath():将此File转换为路径名字符串 String getName():返回由此File表示的文件或目录的名称 long length():返回由此File表示的文件的长度
public static void main(String[] args) { //String getAbsolutePath():返回此File的绝对路径名字字符串 File file = new File("E:\\s.txt"); String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath); //不管路径是目录还是文件返回的都是绝对路径 File file1 = new File("s.txt"); String absolutePath1 = file1.getAbsolutePath(); System.out.println(absolutePath1); //String getPath():将此File转换为路径名字符串 File file2 = new File("E:\\file\\bb.txt"); String parent = file2.getParent(); System.out.println(parent); //String getName():返回由此File表示的文件或目录的名称 File file3 = new File("E:\\file\\bb.txt"); String name = file3.getName(); System.out.println(name); //long length():返回由此File表示的文件的长度 File file4 = new File("E:\\file\\bb.txt"); System.out.println(file4.length()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)