代码示例 |
public class FileClient1 { public static void main(String[] args) throws IOException { // File file = new File("D:\\test\\a.txt"); // long ft = file.lastModified(); // Date d = new Date(ft); // System.out.println(d); // createNewFile("ab.txt"); // deleteFile("d:\\test\\ab"); // existsFile("d:\\test\\ab"); // getAbsolutePath("d:\\test\\ab.txt"); // getFileName("d:\\test\\ab.txt"); // getParent("d:\\test\\ab.txt"); // isDirectory("d:\\test"); // isFile("d:\\test\\ab.txt"); // list("d:\\test"); // listFiles("d:\\test"); // mkdir("d:\\test\\dd"); // mkdirs("d:\\tests\\dd"); renameTo("d:\\test\\a2.txt", "d:\\test\\a2.bat"); // setReadonly("d:\\test\\a2.txt"); }
/** * 创建一个新文件 * @param path * @throws IOException */ public static void createNewFile(String path) throws IOException { File file = new File(path); boolean createFlag = file.createNewFile(); System.out.println(createFlag); }
/** * 删除一个文件 * @param path * @throws IOException */ public static void deleteFile(String path) throws IOException { File file = new File(path); boolean delteFlag = file.delete(); // 返回true表示删除成功 false表示删除失败,失败的原因大部分是文件不存在 System.out.println(delteFlag); }
/** * 判断一个文件后者路径是否存在 * @param path */ public static void existsFile(String path) { File file = new File(path); System.out.println(file.exists()); }
/** * 获取文件的绝对路径 * @param path */ public static void getAbsolutePath(String path) { File file = new File(path); System.out.println(file.getAbsolutePath()); }
/** * 获取文件名 * @param path */ public static void getFileName(String path) { File file = new File(path); System.out.println(file.getName()); }
/** * 获取父路径 */ public static void getParent(String path) { File file = new File(path); System.out.println(file.getParent()); }
/** * 判断一个文件对象是否是目录 */ public static void isDirectory(String path) { File file = new File(path); System.out.println(file.isDirectory()); }
/** * 判断一个文件对象是否是文件 * @param path */ public static void isFile(String path) { File file = new File(path); System.out.println(file.isFile()); }
/** * 获取文件目录下的所有文件和目录清单 * @param path */ public static void list(String path) { File file = new File(path); String[] files = file.list(); for (String fileName : files) { System.out.println(fileName); } }
/** * 获取文件目录下的所有文件和目录清单(文件名) * @param path */ public static void listFiles(String path) { File file = new File(path); File[] files = file.listFiles(); for (File fileName : files) { System.out.println(fileName.getAbsolutePath()); } }
/** * 创建目录 * @param path */ public static void mkdir(String path) { File file = new File(path); System.out.println(file.mkdir()); }
/** * 创建目录(如果父目录不存在,则创建父目录) * @param path */ public static void mkdirs(String path) { File file = new File(path); System.out.println(file.mkdirs()); }
/** * 文件重命名 * @param oldFileName * @param newFileName */ public static void renameTo(String oldFileName, String newFileName) { File oldFile = new File(oldFileName); File newFile = new File(newFileName); System.out.println(oldFile.renameTo(newFile)); }
/** * 设置文件只读 * @param path */ public static void setReadonly(String path) { File file = new File(path); System.out.println(file.setReadOnly()); }
} |
文件常用的操作方法
最新推荐文章于 2023-02-18 17:44:29 发布