读取txt工具类

public class ReadTxtUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReadTxtUtil.class);
    public static String readTxtFile(String filePath) {
        String content = null;
        File file = new File(filePath);
        if (file.isFile() && file.exists() && file.canRead()) {
            try (Reader reader = new InputStreamReader(new FileInputStream(filePath), "UTF-8")) {
                StringBuffer sb = new StringBuffer();
                char[] tempchars = new char[1024];
                while (reader.read(tempchars) != -1) {
                    sb.append(tempchars);
                }
                content = String.valueOf(sb);
                content.replace("\r", "");
                content.replace("\t", "");
                content.replace("\n", "");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            LOGGER.error("NoFindFile License");
        }
        LOGGER.info("read license success");
        return content.trim();
    }

}
发布了116 篇原创文章 · 获赞 107 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_35385687/article/details/102637475
今日推荐