java根据文件路径读取文件内容(txt、properties)

工具代码:

/**
     * @Title: readTxt 
     * @Description: 读取txt文件内容
     * @param filePath
     * @return
     */
    public static String readTxt(String filePath) {// D:\\a.txt
        StringBuilder result = new StringBuilder();
        try {
//          BufferedReader bfr = new BufferedReader(new FileReader(new File(filePath)));
            BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = bfr.readLine()) != null) {
                result.append(lineTxt).append("\n");
            }
            bfr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

猜你喜欢

转载自blog.csdn.net/wgq3773/article/details/80763628