2014-03-07 37 views
0

我在寫一個方法,允許我在文件中的特定點輸入一行,例如.txt或.vbs腳本。我遇到的問題是寫回部分,輸出文件是空白的 - 不包含我的ArrayList scriptCollection的條目。這是我的測試方法代碼;在Java中的特定行修改文件

public void testMethod()throws Exception 
{ 

    BufferedReader br = new BufferedReader(new FileReader("C:/Users/jchild/Desktop/PrintScript.vbs")); 
    int indexNo = 1; 
    int appendAt=0; 
    String line;    
    while((line = br.readLine()) != null) 
    {    
     scriptCollection.add(line); 
     if(line.contains("Add at this point")) 
     { 
      System.out.println("Successfully read and compared"); //this is just for test output 
      appendAt = appendAt + indexNo; 
     } 
     indexNo++; 
    } 
    br.close(); 
    scriptCollection.add(appendAt++,"Appended here"); 
    System.out.println(scriptCollection.toString()); //this is just for test output 

    //here's what's causing the problem 
    FileOutputStream fos = new FileOutputStream("C:/Users/jchild/Desktop/PrintScript.txt"); 
    PrintWriter is = new PrintWriter(fos); 
    for(String temp : scriptCollection) 
    { 
     is.println(temp); 
    }      
    scriptCollection.clear(); 
} 
+1

1)您需要刷新並關閉輸出流。 2)你是否檢查這行是否是'''is.println(temp);'''是否執行了一些數據? – NeplatnyUdaj

+0

錯誤信息..? –

+0

嘗試:'is.close();' – whiskeyspider

回答

1

您必須關閉流。

+1

@DummyCode其實,這是正確的答案。 – whiskeyspider