package com.mashensoft;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
/**
* java 系统操作
*
* @author PeicongHe
*
*/
public class WindowOpration {
/**
* 新建文、文件夹
*/
public static void test1() {
File createfile = new File("src/f.txt");
// File(String pathname)
File makedirection = new File("src/fff");
try {
System.out.println(createfile.createNewFile());
// boolean createNewFile
System.out.println("创建文件成功");
System.out.println(makedirection.mkdirs());
// boolean createNewFile
System.out.println("创建文件夹成功");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除文件、文件夹
*/
public static void test2() {
File deletefile = new File("src/axxx.txt");
System.out.println(deletefile.delete());
}
/**
* 修改文件名
*/
public static void test3() {
File renamefile = new File("src/f.txt");
System.out.println(renamefile.renameTo(new File("src/newF.txt")));
// .rename(File dest)
}
/**
* 查看文件、文件夹的权限 .canRead()/.canWrite/.canExecute 修改文件、文件夹的权限 .set
*/
public static void test4() {
File canfile = new File("src/a.txt");
System.out.println("查看权限");
// boolean canRead canWrite canExecute
System.out.print(canfile.canRead() + " ");
System.out.print(canfile.canWrite() + " ");
System.out.println(canfile.canExecute());
System.out.println("修改权限");
System.out.print("修改执行权限 : " + canfile.setExecutable(false) + " ");
System.out.println("修改读写权限 : " + canfile.setReadOnly() + " ");
System.out.println("查看权限");
System.out.print(canfile.canRead() + " ");
System.out.print(canfile.canWrite() + " ");
System.out.println(canfile.canExecute());
}
/**
* 搜索文件(自定义) list()
*/
public static void test5() {
File file = new File("./");
// 当前文件夹
String[] files = file.list();
// 设置数组,存放文件名
for (int i = 0; i < files.length; i++) {
File temp = new File(files[i]);
// 临时存放文件,用于逐个文件名判断是否为文件夹
if (temp.isDirectory()) {
System.out.println("d " + files[i] + "这是个文件夹");
} else {
System.out.println("- " + files[i] + "这是个文件");
}
}
}
/**
* 用系统提供的方法过滤文件
*/
public static void test6() {
File file = new File("./");
// 当前文件夹
File files[] = file.listFiles(new FileFilter() {
public boolean accept(File pathname) {
// 判断文件后缀是否符合,符合则返回true,不符合就返回false
// 用于接收文件名
if (pathname.getName().endsWith("project")) {
return true;
}
return false;
}
});
for (int i = 0; i < files.length; i++) {
// 输出文件名
System.out.println(files[i].getName());
}
}
public static void main(String[] args) {
test6();
}
}
java 系统操作
最新推荐文章于 2021-11-28 11:11:58 发布