JAVA IO流:File类的获取功能
1. getName(); 返回String,从封装的File对象中获取路径中的最后部分,要么是文件名,要么是目录名,路径是否真的在硬盘中存在无所谓。
File file = new File("c:\\acb.txt");
file.getName();返回acb.txt
File file = new File("c:\abc");
file.getName();返回abc
2. getPath(); 从封装的File对象中,获取String类型的路径,功能有点类似于toString(); 也可以这样解释:将File对象转成String字符串
File file = new File("c:\\abc\\a.txt");
String path = file.getPath();
3. length(); 获取文件的长度,字节数。只能获取文件的长度,而不能获取目录的长度
File file = new File("c:\\AMD\\InternetReadFileEx.txt");
long length = file.length();
结果为2455349字节
4.
getAbsolutePath(); 获取字符串形式的绝对路径
getAbsoluteFile(); 获取File对象形式的绝对路径
在eclipse中,相对路径是以workspace\项目文件夹 为根目录的。
5.
getParentPath(); 获取字符串形式的父路径
getParentFile(); 获取File对象形式的父路径
6. File类静态方法File.listRoots(); 返回系统所有根目录的File数组
7. list()方法,返回一个String[]数组,相当于遍历目录
listFiles();遍历目录,返回一个File对象的数组
浙公网安备 33010602011771号