2017-02-16 36 views
0

我有有文字如下文本文件:阅读文本文件的变量和更新的赋值

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(); 
    } 
} 

}

+1

请显示您的代码。 –

+1

什么意思是“文件中的空格不相等”?你期望哪个输出,你会得到什么?并至少显示代码,它会写出你的(错误的)输出。 – IQV

回答

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()); 
    } 
+0

它在ecdbVersion和ReleaseVersion之前没有空间时正常工作,但正如我在文本文件空间中显示的那样,在edbVersion之前没有,所以它不适用于此:(@Lyle –

+0

因此,您希望它具有完全相同的输出,空格?还是可以没有空格? – Lyle

+0

文本文件应该保留,因为它只是版本应该改变@Lyle –

0

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