替换INI文件中的特定文本

时间:2018-05-30 10:32:00

标签: java ini

在INI文件中查找特定STRING并将其替换为新文件的最佳方法是什么。

让我们在.ini文件中的某个地方说我有这些行:

Something = Something_50054.txt
Something1 = Something1_50054.txt
Something2 = Something2_50054.txt
Something3 = Something3_50054.txt
Something4 = Something4_50054.txt
Something5 = Something5_50054.txt

现在我需要找到" Something1_500"在INI中并用新字符串替换它。 (示例:替换" Something_50054.txt"使用" Something_50055.txt")

这是我目前拥有的代码示例:

for (int i = 0; i < myfileList.length; i++) { //myfileList - list path to ini file
    try {
        if (myfileList[i].trim() != null) {
            File file = new File(myfileList[i].trim());
            BufferedReader br = new BufferedReader(new FileReader(file));
            String st;
            while ((st = br.readLine()) != null) {
                //Should I do it here?
            }
        }
    } catch (Exception e) {

    }

解决方案:

                Path path = Paths.get(myfileList[i].trim());
                Stream<String> lines = Files.lines(path);
                List<String> replaced = lines.map(line -> line.replaceAll("DBGPS", "TEST123")).collect(Collectors.toList());
                Files.write(path, replaced);
                lines.close();
                System.out.println("Find and Replace done!!!");

0 个答案:

没有答案