FileUtils

package com.zxwa.ntmss.process.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FileUtil {

    public static void main(String args[]) {
        checkRepeat("D://leijirong1.txt");
    }

    public static Set<String> checkRepeat(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        Set<String> list = new HashSet<String>();
        try {
            System.out.println("以行为单位读取文件内容,一次读一行");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读一行,读入null时文件结束
            while ((tempString = reader.readLine()) != null) {

                if (list.contains(tempString)) {
                    System.out.println(tempString);
                } else {
                    list.add(tempString);
                }
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
    }

    public static Set<String> readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        Set<String> list = new HashSet<String>();
        try {
            System.out.println("以行为单位读取文件内容,一次读一行");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读一行,读入null时文件结束
            while ((tempString = reader.readLine()) != null) {
                list.add(tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
    }


    public static void writeFileByLines(String fileName, List<String> content) {
        File file = new File(fileName);
        FileWriter fw = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file, true);
            for (String str : content) {
                fw.append(str).append("
");
            }
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeFileByLines(String fileName, String content) {
        File file = new File(fileName);
        FileWriter fw = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file, true);
            fw.append(content).append("
");
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            file.delete();
        }

    }


    /**
     * 将JSON数据格式化并保存到文件中
     *
     * @param t        需要输出的json数
     * @param filePath 输出的文件地址
     * @return
     */
    public static <T> boolean createJsonFile(T t, String filePath) {
        String content = JSON.toJSONString(t, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        boolean flag = true;
        try {
            // 保证创建一个新文件
            File file = new File(filePath);
            if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(content);
            write.flush();
            write.close();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * 读取json 格式的文件
     *
     * @param Path 输出的文件地址
     * @return
     */
    public JSONArray ReadFile(String Path) {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return JSONObject.parseArray(laststr);
    }
}
故乡明
原文地址:https://www.cnblogs.com/luweiweicode/p/14243610.html