标签:cal 后缀 成功 关闭 默认 随机数 auto exit 获取
本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识
File类是java.io包下代表与平台无关的文件或目录,如果希望在程序中操作文件或目录,可以通过File类来完成,File类可以使用相对路径或绝对路径来创建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
文件检测相关的方法:
代码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可执行
目录操作的相关方法:
代码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
根路径:
/
获取文件常规信息:
文件操作相关方法:
代码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
标签:cal 后缀 成功 关闭 默认 随机数 auto exit 获取
原文地址:http://www.cnblogs.com/baoliyan/p/6219130.html