2014-02-07 108 views
2

因此,我已經瀏覽了此論壇上的所有建議,但他們中的任何一個都不能真正幫助我找到所需的答案。從另一個文件中更改文件中的特定行

這是我的主代碼: 我遺漏了工具,因爲這工作正常。

public class Management { 
    private static int Price1 = 0; 
    private static int Price2 = 0; 

    public static void main(String[] args) { 
     try 
     { 
      Scanner file = new Scanner(new File("friendsfile")); 
      String s; 

      while(file.hasNext()) 
      { 
       s = file.nextLine(); 
       if(s.contains("House 1")) 
       { 
        Price1 = getPrice(s); 
       } 

       if(s.contains("House 2")) 
       { 
        Price2 = getPrice(s); 
       } 
      } 
      Filewriter fw = new Filewriter(); 
      fw.fileWriter(Price1,Price2); 
     } 
     catch(FileNotFoundException fnfe) 
     { 
      System.out.println("File not found"); 
     } 
     catch(Exception e) 
     { 
      System.out.println("HERE IS AN ERROR MATE!"); 
     } 
    } 

    private static int getPrice(String s) 
    { 
     StringTokenizer st = new StringTokenizer(s,";"); 

     st.nextToken(); 
     int price1 = Integer.parseInt(st.nextToken().replace(" ","")); 
     return price1; 
    } 
} 

現在FileWriter的是這樣的:

public class Filewriter { 

    public static void fileWriter(int price1, int price2) 
    { 
     System.out.println("Ye"); 
     final String FILE_PATH = "housesfile"; 
     final String MARKER_LINE1 = "price1"; 
     final String TEXT_TO_ADD1 = "price1: "+price1; 
     final String MARKER_LINE2 = "price2"; 
     final String TEXT_TO_ADD2 = "price2: "+price2; 

     List<String> fileLines = new ArrayList<String>(); 
     Scanner scanner = null; 
     try { 
      scanner = new Scanner(new File(FILE_PATH)); 
      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       fileLines.add(line); 
       if (line.trim().equalsIgnoreCase(MARKER_LINE1)) { 
        fileLines.add(TEXT_TO_ADD1); 
       } 
       if (line.trim().equalsIgnoreCase(MARKER_LINE2)) { 
        fileLines.add(TEXT_TO_ADD2); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      if (scanner != null) { 
       scanner.close(); 
      } 
     } 

     PrintWriter pw = null; 
     try { 
      pw = new PrintWriter(new File(FILE_PATH)); 
      for (String line : fileLines) { 
       pw.println(line); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      if (pw != null) { 
       pw.close(); 
      } 
     } 
    } 

} 

基本上我想:

House 1: price; 520592 
House 2: price; 342038 

從一個文件中那些價格數字,轉換到這個文件,這是另外一個。

house #1 
owner: -NAME- 
price1: 500000 
info: 3 bedrooms, 3 bathrooms. 
last changed: - DATE - 
rented: no 

house #2 
owner: -NAME- 
price2: 150000 
info: 3 bedroom, 3 bathroom. 
last changed: -date- 
rented: no 

現在,它不會改變文件中的任何內容,爲什麼?請幫忙。

回答

0

這是因爲您在Filewriter課程中使用了equalsIgnoreCase(),並將其與price1,price2進行比較。但是,您的文件的值爲price1: 500000

因此,將equalsIgnoreCase()更改爲,它應該工作。

更好

,既改變下限或上限的情況下再搭配: line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())

因此,您在FILEWRITE類內循環將是:

while (scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    fileLines.add(line); 
    if (line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())) { 
     fileLines.add(TEXT_TO_ADD1); 
    } 
    if (line.trim().toLowerCase().contains(MARKER_LINE2.toLowerCase())) { 
     fileLines.add(TEXT_TO_ADD2); 
    } 
} 

編輯: 嘗試和您的代碼工作,除了它還包括以前添加的price1,price2值,其中y ou可能希望排除/覆蓋(在修改後的if語句本身之上):

house #1 
owner: -NAME- 
price1: 500000 
price1: 520592 
info: 3 bedrooms, 3 bathrooms. 
last changed: - DATE - 
rented: no 

house #2 
owner: -NAME- 
price2: 150000 
price2: 342038 
info: 3 bedroom, 3 bathroom. 
last changed: -date- 
rented: no 
+1

非常感謝你,那完美無缺!我剛添加了這個: if(line.trim()。toLowerCase()。contains(MARKER_LINE1.toLowerCase())){ fileLines.remove(line); fileLines.add(TEXT_TO_ADD1); 對他們來說,刪除前一個並添加新的,現在一切正常! = D太棒了! – user3284750

+0

@ user3284750很酷,你可以通過點擊關閉的勾號來接受這個答案 – PopoFibo

相關問題