本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识
File类是java.io包下代表与平台无关的文件或目录,如果希望在程序中操作文件或目录,可以通过File类来完成,File类可以使用相对路径或绝对路径来创建File对象,默认情况下,系统总是根据用户的工作路径来解释相对路径
访问文件名相关的方法:
- String getName():返回此File对象所表示的文件名或路径名(如果是路径,则返回最后一级子路径名)
- String getPath():返回此File对象所对应的路径名
- String getAbsolutePath():返回此File对象所对应的绝对路径名
- File getAbsoluteFile():返回此File对象的绝对路径
- String getParent():返回此File对象所对应目录的父目录名
代码1-1
import java.io.File; public class VisitFile { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } for (String arg : args) { visitFileInfo(arg); } } public static void visitFileInfo(String path) { // 以当前路径来创建一个file对象 File file = new File(path); // 返回此File对象的绝对路径 File absoluteFile = file.getAbsoluteFile(); System.out.println("file路径为:" + file.getPath()); System.out.println("absoluteFile路径为:" + absoluteFile.getPath()); System.out.println("file名称:" + file.getName()); System.out.println("absoluteFile名称:" + absoluteFile.getName()); System.out.println("file绝对路径:" + file.getAbsolutePath()); System.out.println("absoluteFile绝对路径:" + absoluteFile.getAbsolutePath()); System.out.println("file父目录:" + file.getParent()); System.out.println("absoluteFile父目录:" + absoluteFile.getParent()); System.out.println(); } }
使用tree命令树级显示/home/software/apache-tomcat-1/conf/目录的文件
root@lejian:/home/software/.io# tree /home/software/apache-tomcat-1/conf/ /home/software/apache-tomcat-1/conf/ ├── Catalina │ └── localhost ├── catalina.policy ├── catalina.properties ├── context.xml ├── logging.properties ├── server.xml ├── tomcat-users.xml ├── tomcat-users.xsd └── web.xml 2 directories, 8 files
运行代码1-1,将.(当前路径)、/home/software/apache-tomcat-1/conf/context.xml、/home/software/apache-tomcat-1/conf/Catalina/ 作为运行参数传入程序。可以看到,当使用绝对路径作为File对象的构造参数时,file和absoluteFile的返回结果是一致的,而使用相对路径时,则不然。另外,在Linux上,绝对路径以/开头,而在Windows上,绝对路径以分盘符开头(如从C:、E:、F:)
代码1-1运行结果:
root@lejian:/home/software/.io# java VisitFile . /home/software/apache-tomcat-1/conf/context.xml /home/software/apache-tomcat-1/conf/Catalina/ file路径为:. absoluteFile路径为:/home/software/.io/. file名称:. absoluteFile名称:. file绝对路径:/home/software/.io/. absoluteFile绝对路径:/home/software/.io/. file父目录:null absoluteFile父目录:/home/software/.io file路径为:/home/software/apache-tomcat-1/conf/context.xml absoluteFile路径为:/home/software/apache-tomcat-1/conf/context.xml file名称:context.xml absoluteFile名称:context.xml file绝对路径:/home/software/apache-tomcat-1/conf/context.xml absoluteFile绝对路径:/home/software/apache-tomcat-1/conf/context.xml file父目录:/home/software/apache-tomcat-1/conf absoluteFile父目录:/home/software/apache-tomcat-1/conf file路径为:/home/software/apache-tomcat-1/conf/Catalina absoluteFile路径为:/home/software/apache-tomcat-1/conf/Catalina file名称:Catalina absoluteFile名称:Catalina file绝对路径:/home/software/apache-tomcat-1/conf/Catalina absoluteFile绝对路径:/home/software/apache-tomcat-1/conf/Catalina file父目录:/home/software/apache-tomcat-1/conf absoluteFile父目录:/home/software/apache-tomcat-1/conf
文件检测相关的方法:
- boolean exists():判断File对象所对应的文件或目录是否存在
- boolean canWrite():判断File对象所对应的文件是否可写
- boolean canRead():判断File对象所对应的文件是否可读
- boolean canExecute():判断File对象所对应的文件是否执行
- boolean isDirectory():判断File对象所对应的文件是否是目录
- boolean isAbsolute():判断File对象所对应的文件或目录是否是绝对路径
代码1-2
import java.io.File; public class DetectionFile { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } for (String arg : args) { DetectionFileInfo(arg); } } public static void DetectionFileInfo(String path) { File file = new File(path); if (!file.exists()) { System.out.println(file + "并不存在\n"); return; } if (file.isAbsolute()) { System.out.println(file + "是绝对路径"); } else { System.out.println(file + "不是绝对路径"); } if (file.isFile()) { System.out.println(file + "为文件"); } if (file.isDirectory()) { System.out.println(file + "为目录"); } if (file.canRead()) { System.out.println(file + "可读"); } else { System.out.println(file + "不可读"); } if (file.canWrite()) { System.out.println(file + "可写"); } else { System.out.println(file + "不可写"); } if (file.canExecute()) { System.out.println(file + "可执行"); } else { System.out.println(file + "不可执行"); } System.out.println(); } }
显示当前路径下以test开头的文件,它们分别具有不同的读写执行权限
root@lejian:/home/software/.io# ls -l test* -rw-r----- 1 root root 0 Dec 25 11:29 test1 -rw-r--r-- 1 root root 0 Dec 25 11:29 test2 -rw-r---w- 1 root root 0 Dec 25 11:29 test3 -rw-r--rw- 1 root root 0 Dec 25 11:29 test4 -rw-r--rwx 1 root root 0 Dec 25 11:29 test5
代码1-2运行结果:
linfeng@lejian:/home/software/.io$ /home/software/java/bin/java DetectionFile /home/software/.io/badFile test1 test2 test3 test4 /home/software/.io/test5 /home/software/.io/badFile并不存在 test1不是绝对路径 test1为文件 test1不可读 test1不可写 test1不可执行 test2不是绝对路径 test2为文件 test2可读 test2不可写 test2不可执行 test3不是绝对路径 test3为文件 test3不可读 test3可写 test3不可执行 test4不是绝对路径 test4为文件 test4可读 test4可写 test4不可执行 /home/software/.io/test5是绝对路径 /home/software/.io/test5为文件 /home/software/.io/test5可读 /home/software/.io/test5可写 /home/software/.io/test5可执行
目录操作的相关方法:
- boolean mkdir():创建一个File对象所对应的目录,如果创建成功返回true,否则返回false
- String[] list():列出File对象的所有子文件名和路径名,返回String数组
- File[] listFiles():列出File对象的所有子文件和路径,返回File数组
- static File[] listRoots():列出系统所有根路径
代码1-3
import java.io.File; public class ListFile { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } File file = new File(args[0]); list(0, file.listFiles()); String[] fileNames = file.list(); System.out.println(); System.out.println(file + "下文件:"); for (String fileName : fileNames) { System.out.println(fileName); } System.out.println(); System.out.println("根路径:"); File[] roots = File.listRoots(); for (File root : roots) { System.out.println(root); } } public static void list(int level, File[] files) { if (files == null) { return; } for (File file : files) { for (int i = 0; i < level; i++) { System.out.print("\t"); } System.out.println(file); if (file.isDirectory()) { list(level + 1, file.listFiles()); } } } }
将/home/software/apache-tomcat-1/conf/传入代码1-3然后运行,文章之前已用tree命令打印了/home/software/apache-tomcat-1/conf/ 下文件
代码1-3运行结果:
root@lejian:/home/software/.io# java ListFile /home/software/apache-tomcat-1/conf/ /home/software/apache-tomcat-1/conf/tomcat-users.xml /home/software/apache-tomcat-1/conf/server.xml /home/software/apache-tomcat-1/conf/context.xml /home/software/apache-tomcat-1/conf/tomcat-users.xsd /home/software/apache-tomcat-1/conf/Catalina /home/software/apache-tomcat-1/conf/Catalina/localhost /home/software/apache-tomcat-1/conf/catalina.policy /home/software/apache-tomcat-1/conf/logging.properties /home/software/apache-tomcat-1/conf/catalina.properties /home/software/apache-tomcat-1/conf/web.xml /home/software/apache-tomcat-1/conf下文件: tomcat-users.xml server.xml context.xml tomcat-users.xsd Catalina catalina.policy logging.properties catalina.properties web.xml 根路径: /
获取文件常规信息:
- long lastModified():返回文件的最后修改时间
- long length():返回文件内容的长度
文件操作相关方法:
- boolean createNewFile():当File对象所对应的文件不存在时,创建一个File对象所指定的新文件,如果创建成功则返回true,否则返回false
- boolean delete():删除File对象所对应的文件或路径
- static File createTempFile(String prefix, String suffix, File directory):在指定的File目录下创建一个临时的空文件,使用给定前缀、系统生成的随机数和给定后缀作为文件名,如果没有传入File目录对象,则默认在临时文件目录创建临时文件
- void deleteOnExit():File对象会在JVM退出时删除所指定的文件或目录
- boolean renameTo(File dest):重命名此File对象所对应的文件或目录,如果命名成功则返回true,否则返回false
代码1-4
import java.io.File; public class RenameFile { public static void main(String[] args) { if (args == null || args.length != 2) { throw new RuntimeException("请输入两个路径"); } File file = new File(args[0]); File[] files = file.listFiles(); for (File f : files) { System.out.println(f); } for (File f : files) { f.renameTo(new File(args[1] + "/" + f.getName() + ".rename")); } file = new File(args[1]); files = file.listFiles(); for (File f : files) { System.out.println(f); } } }
当把./directory1/ ./directory2/作为参数传入代码1-4后,程序会将directory1下的文件的路径修改到directory2底下
代码1-4运行结果:
root@lejian:/home/software/.io# ls -l directory1/ total 8 drwxr-xr-x 2 root root 4096 Dec 25 15:58 test1 drwxr-xr-x 2 root root 4096 Dec 25 15:58 test2 -rw-r--r-- 1 root root 0 Dec 25 15:58 test3 -rw-r--r-- 1 root root 0 Dec 25 15:58 test4 -rw-r--r-- 1 root root 0 Dec 25 15:58 test5 root@lejian:/home/software/.io# ls -l directory2/ total 0 root@lejian:/home/software/.io# java RenameFile ./directory1/ ./directory2/ ./directory1下文件: ./directory1/test5 ./directory1/test3 ./directory1/test2 ./directory1/test4 ./directory1/test1 ./directory2下文件: ./directory2/test3.rename ./directory2/test5.rename ./directory2/test2.rename ./directory2/test1.rename ./directory2/test4.rename root@lejian:/home/software/.io# ls -l directory1/ total 0 root@lejian:/home/software/.io# ls -l directory2/ total 8 drwxr-xr-x 2 root root 4096 Dec 25 15:58 test1.rename drwxr-xr-x 2 root root 4096 Dec 25 15:58 test2.rename -rw-r--r-- 1 root root 0 Dec 25 15:58 test3.rename -rw-r--r-- 1 root root 0 Dec 25 15:58 test4.rename -rw-r--r-- 1 root root 0 Dec 25 15:58 test5.rename
代码1-5
import java.io.File; import java.io.IOException; public class CreateFile { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } File file = new File(args[0]); boolean res = file.mkdir(); if (!res) { System.out.println("目录创建失败,程序退出"); return; } try { long millis = System.currentTimeMillis(); res = new File(args[0] + "/file" + millis).createNewFile(); if (res) { System.out.println("file" + millis + "文件创建成功"); } for (int i = 0; i < 5; i++) { File f = File.createTempFile("tmp", ".tmp", file); if (f.exists()) { System.out.println(f.getName() + "创建成功"); } if (i % 2 == 0) { System.out.println("当JVM关闭时" + f.getName() + "将自动删除"); f.deleteOnExit(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
当把/home/software/directory作为参数传入代码1-5,程序会先在/home/software/目录下创建directory目录,然后在进行一系列文件的创建
代码1-5运行结果:
root@lejian:/home/software/.io# java CreateFile /home/software/directory file1482653956423文件创建成功 tmp8886044583353552690.tmp创建成功 当JVM关闭时tmp8886044583353552690.tmp将自动删除 tmp9125889364503043964.tmp创建成功 tmp8268038408100305429.tmp创建成功 当JVM关闭时tmp8268038408100305429.tmp将自动删除 tmp7927875863775247323.tmp创建成功 tmp6962749496562864357.tmp创建成功 当JVM关闭时tmp6962749496562864357.tmp将自动删除 root@lejian:/home/software/.io# ls -l /home/software/directory/ total 0 -rw-r--r-- 1 root root 0 Dec 25 16:19 file1482653956423 -rw-r--r-- 1 root root 0 Dec 25 16:19 tmp7927875863775247323.tmp -rw-r--r-- 1 root root 0 Dec 25 16:19 tmp9125889364503043964.tmp