读取记事本里面的内容

    xiaoxiao2025-04-26  15

    现在配置文件越来越多,把可能改的东西放到一个文件里面,到时只要修改文件就可以了,不需要查找代码,很方便也很高效

    /*** 读取txt里的单行内容* @param filePath  文件路径*/public static String readTxtFile(String fileP) {try { String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../";//项目路径filePath = filePath.replaceAll("file:/", "");filePath = filePath.replaceAll("%20", " ");filePath = filePath.trim() + fileP.trim();if(filePath.indexOf(":") != 1){filePath = File.separator + filePath;}String encoding = "utf-8";File file = new File(filePath);if (file.isFile() && file.exists()) { // 判断文件是否存在InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式BufferedReader bufferedReader = new BufferedReader(read);String lineTxt = null;while ((lineTxt = bufferedReader.readLine()) != null) {return lineTxt;}read.close();}else{System.out.println("找不到指定的文件,查看此路径是否正确:"+filePath);}} catch (Exception e) {System.out.println("读取文件内容出错");}return "";}

    转载请注明原文地址: https://ju.6miu.com/read-1298485.html
    最新回复(0)