import java.io.File;
public class Main {
public static void main(String[] args) {
File f = new File("demo05/src/test.txt");
System.out.println(f);
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.isFile());
System.out.println(f.isDirectory());
System.out.println(f.canRead());
}
}
// 创建临时文件
public class Main {
public static void main(String[] args) throws IOException {
File f = File.createTempFile("tmp-", ".txt"); // 提供临时文件的前缀和后缀
f.deleteOnExit(); // JVM退出时自动删除
System.out.println(f.isFile());
System.out.println(f.getAbsolutePath());
}
import java.io.File;
import java.io.FilenameFilter;
public class GetFileDir {
public static void main(String[] args) {
File f = new File("C:\\Windows");
File[] fs1 = f.listFiles();// 列出所有的文件和子目录
printFiles(fs1);
File[] fs2 = f.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".exe");//返回True表示接受该文件
}
});
printFiles(fs2);
}
private static void printFiles(File[] files) {
System.out.println("==========================================");
if (files != null) {
for (File f : files) {
System.out.println(f);
}
}
System.out.println("==========================================");
}
}
// path路径
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathTs {
public static void main(String[] args) {
// 构造一个Path对象
Path p1 = Paths.get(".", "project", "study");
System.out.println(p1);
//转化为绝对路径
Path p2 = p1.toAbsolutePath();
System.out.println(p2);
//转换为规范路径
Path p3 = p2.normalize();
System.out.println(p3);
//转换为File对象
File f = p3.toFile();
System.out.println(f);
for (Path p : Paths.get("..").toAbsolutePath()) {
System.out.println(" " + p);
}
}
}