2017-02-16 37 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 *「); 。如果它不起作用,請告訴我,我會爲您提供完整的解決方案。