JAVA获取txt文件的所有行

1、获取txt文件的所有行

/** 
     * 获取txt的所有行
     * @author xxx
     * @date 2023/2/2 16:09
     * @param file 
     * @return List<String> 
     */
    public static List<String> getAllLineListByTxt(File file) throws IOException {
        List<String> readAllLines = null;
        try {
            readAllLines = Files.readAllLines(Paths.get(file.getCanonicalPath()), Charset.forName("GBK"));
        } catch (Exception e) {
            e.printStackTrace();
            readAllLines = Files.readAllLines(Paths.get(file.getCanonicalPath()),Charset.forName("UTF-8"));
        }
        return readAllLines;
    }

  

posted @ 2023-02-02 16:15  信铁寒胜  阅读(152)  评论(0编辑  收藏  举报