package com.test.file; import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 文件测试类,非工具类 */ public class FileTest { /** * 通过路径名创建一个文件夹 * @param folderPath * @return 当且仅当创建成功时,返回code为true */ public static Result createFolder(String folderPath){ File file = new File(folderPath); if(!file.exists()){ if(!file.mkdir()){ return Result.fail("创建本地文件夹失败"); } } return Result.success(); } /** * 根据文件名创建一个File类,如果该文件不存在则创建一个。 * @param filePath * @return 当且仅当创建成功时,返回code为true * @throws IOException */ public static Result createFile(String filePath) throws IOException { File file = new File(filePath); if(!file.exists()){ if(!file.createNewFile()){ return Result.fail("创建本地文件失败"); } } return Result.success(); } /** * 删除一个文件或文件夹 * @param path * @return 当且仅当删除成功时,返回code为true */ public static Result delete(String path){ File file = new File(path); if(!file.exists()){ return Result.fail("文件/文件夹并不存在"); } if(!file.delete()){ return Result.fail("文件/文件夹存在,但在删除文件/文件夹时失败"); } return Result.success(); } /** * 清空一个文件夹内所有内容 * @param file * @return */ public static Result cleanFolder(File file){ File[] files = file.listFiles(); for(int i = 0; i< files.length; i++){ if(files[i].isDirectory()){ cleanFolder(files[i]); } if(!files[i].delete()){ return Result.fail("删除文件/文件夹失败,失败文件/文件夹:" + files[i].getPath()); } } return Result.success(); } /** * 删除一个文件夹,包括其中的所有内容 * @param folder * @return */ public static Result deleteFolder(File folder){ if(!folder.isDirectory()) { return Result.fail("该File不属于文件夹"); } if(!folder.exists()) { return Result.fail("文件夹并不存在,无法删除"); } //清理文件夹 Result deleteFolderIncludeResult = cleanFolder(folder); if(deleteFolderIncludeResult.code == Result.FAILURE_CODE){ return Result.fail(deleteFolderIncludeResult.msg); } if(!folder.delete()){ return Result.fail("文件夹已被清空,但删除文件夹失败"); } return Result.success(); } /** * 逐行读取文件内容 * @param file * @return * @throws IOException */ public static Result fileRead(File file) throws IOException { if(file.isFile()){ return Result.fail("改文件不是一个标准文件"); } FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder stringBuilder = new StringBuilder(); String lineStr = ""; while((lineStr = bufferedReader.readLine()) != null){ stringBuilder.append(lineStr).append(" "); } return ReadFileResult.success(stringBuilder.toString()); } /** * 写入字符串到文件1 * @param path * @param str * @return * @throws IOException */ public static Result fileWrite1(String path, String str) throws IOException { File file = new File(path); FileWriter fileWriter = new FileWriter(file); fileWriter.write(str); return Result.success(); } /** * 写入字符串到文件2 * @param path * @param str * @return * @throws IOException */ public static Result fileWrite2(String path, String str) throws IOException { File file = new File(path); FileOutputStream fileOutputStream = new FileOutputStream(file, true); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write(str); return Result.success(); } /** * 写入到字符串到文件3 * @param path * @param str * @return * @throws IOException */ public static Result fileWrite3(String path, String str) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw"); long fileLength = randomAccessFile.length(); randomAccessFile.seek(fileLength); randomAccessFile.writeBytes(str+" "); randomAccessFile.close(); return Result.success(); } /** * 获取文件属性 * @param filePath */ public static void getFileField(String filePath){ File f = new File(filePath); if (f.exists()) { System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length()); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目录" : "不是目录"); System.out.println(f.canRead() ? "可读取" : "不"); System.out.println(f.canWrite() ? "是隐藏文件" : ""); System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified())); } else { System.out.println(f.getName() + "的属性如下:"); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目录" : "不是目录"); System.out.println(f.canRead() ? "可读取" : "不"); System.out.println(f.canWrite() ? "是隐藏文件" : ""); System.out.println("文件的最后修改日期为:" + new Date(f.lastModified())); } } /** * 获取文件夹下所有文件 * @param folderPath * @return */ public static File[] getFolderIncludeFile(String folderPath){ File folder = new File(folderPath); if(!folder.exists()){ return null; } if(!folder.isDirectory()){ return null; } return folder.listFiles(new FileFilter() { public boolean accept(File pathname) { return !pathname.getName().startsWith("."); } }); } }