读取文本文件变量和更新的指定值

时间:2017-02-16 07:55:59

标签: java file parsing

我有一个文本文件,其文本如下:

emVersion = "1.32.4.0";
 ecdbVersion = "1.8.9.6";
 ReleaseVersion = "2.3.2.0";

如果用户输入emVersion的新值为1.32.5.0然后

,我想通过获取用户的输入来更新版本号 文本文件中的

emVersion将更新为emVersion = "1.32.5.0";

所有这一切我都要用java代码来做。我到目前为止所做的是逐行读取文本文件,然后搜索单词emVersion如果发现断行为单词然后替换令牌1.32.4.0但它不起作用,因为空格在文件中是不相等的。

我写的代码是:

公共类UpdateVariable {

public static void main(String s[]){
    String replace = "1.5.6";
String UIreplace = "\""+replace+"\"";
     File file =new File("C:\\Users\\310256803\\Downloads\\setup.rul");
    Scanner in = null;
    try {
        in = new Scanner(file);
        while(in.hasNext())
        {
            String line=in.nextLine();
            if(line.contains("svEPDBVersion"))
            {
                String [] tokens = line.split("\\s+");
                String var_1 = tokens[0];
                String var_2 = tokens[1];
    String var_3 = tokens[2];
    String var_4 = tokens[3];
    String OldVersion = var_3;
    String NewVersion = UIreplace;
    try{
    String content = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
    content = content.replaceAll(OldVersion, NewVersion);
    IOUtils.write(content, new FileOutputStream(file), StandardCharsets.UTF_8);
    } catch (IOException e) {

    }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

2 个答案:

答案 0 :(得分:0)

    //---this code changes each version's values but the is a option to keep the old value.
    Scanner in = new Scanner(System.in);

    File file = new File("versions.txt");

    ArrayList<String> data = new ArrayList<>();
    String[] arr =
    {
        "emVersion", "ecdbVersion", "releaseVersion"
    };

    String line = "";
    String userInput = "";

    try (BufferedReader br = new BufferedReader(new FileReader(file));)
    {
        while ((line = br.readLine()) != null)
        {
            data.add(line);
        }

        for (int i = 0; i < arr.length; i++)
        {
            System.out.println("Please enter new " + arr[i] + " number or (s) to keep the old value.");
            userInput = in.nextLine();

            line = data.get(i);
            String version = line.substring(0, line.indexOf(" "));


            if (arr[i].equalsIgnoreCase(version))
            {
                arr[i] = line.replace(line.subSequence(line.indexOf("= "), line.indexOf(";")), "= \"" + userInput + "\"");
            }

            if (userInput.equalsIgnoreCase("s"))
            {
                arr[i] = line;
            }

        }

        PrintWriter printWriter = new PrintWriter(new FileWriter(file, false));
        printWriter.println(arr[0]);
        printWriter.println(arr[1]);
        printWriter.println(arr[2]);
        printWriter.close();
    }
    catch (Exception e)
    {
        System.out.println("Exception: " + e.getMessage());
    }

答案 1 :(得分:0)

使用正则表达式,例如: - line.trim()。split(“\ s * = \ s *”); 。如果它不起作用请告诉我,我会为您提供完整的解决方案。