java的文件操做(1)

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("\n");
        }
        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+"\n");
        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(".");
            }
        });
    }


}
相關文章
相關標籤/搜索