📄 文件属性的访问.txt
字号:
import java.io.*;
public class FileDemo{
private static void getFileInfo(File f) { //返回指定文件的基本属性
System.out.println("Absolute path: " + f.getAbsolutePath() +
"\nCan read: " + f.canRead() +
"\nCan write: " + f.canWrite() +
"\ngetName: " + f.getName() +
"\ngetParent: " + f.getParent() +
"\ngetPath: " + f.getPath() +
"\nlength: " + f.length() +
"\nlastModified: " + f.lastModified());
}
private static void getDirectoryInfo(File f) { //返回指定目录下所有文件的名称
String d[] = f.list();
for(int i=0 ; i<d.length ;i++)
System.out.println(d[i]);
}
public static void main(String args[])throws Exception{
String dirname = args[0];
String filename = args[1];
File dir = new File(dirname);
File file = new File(dirname,filename);
if(!dir.exists()){ //判断目录dirname是否存在,如果不存在,则创建该目录
System.out.println("created directory " + dir);
dir.mkdir();
}
System.out.println("directory info:");
getDirectoryInfo(dir);
if(!file.exists()){ //判断目录dirname下的文件filename是否存在,如果不存在,
//则创建该文件,否则,删除该文件
System.out.println("created file " + file);
file.createNewFile();
}
else{
System.out.println("deleting file " + file);
file.delete();
}
System.out.println("file info:" );
getFileInfo(file);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -