package cn.teammodel.utils;

import cn.teammodel.common.ErrorCode;
import cn.teammodel.config.exception.ServiceException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public class FileUtil {

    /**
     * 读取文件信息
     * @param file
     * @return
     */
    public static String readFile(File file) {
        StringBuilder sb = new StringBuilder();
        try {
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
            int ch = 0;
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            return  sb.toString();
        }catch (IOException e)
        {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
        }
    }

}
