2011-07-05 15 views
0

我想知道是否有方法來確定是否有任何流在程序中打開?重寫一個文件,該程序已經使用

我正在使用我的一些代碼和另一些代碼,我的目標是能夠多次寫入同一個文件,每次擦除它並重寫。但是,我認爲在某個地方,屬於這個其他組的代碼可能忘記關閉一個流,或者Java無法處理它,也許?它總是在文件的末尾寫入,而不是在空白文件的開頭。它不會刪除,如果它已被程序打開,我不能重命名它。

如果它是一個開放流問題,我想關閉流(我已經通過代碼,並且似乎無法找到打開的流)。或者,如果Java無法處理它,有沒有一種好的方法(除了製作銷燬方法)能讓我重置/殺死那些重新生成的對象?

或者有沒有辦法可能...設置文件爲空,並刪除它?或者我應該嘗試打開文件,擦除它並將偏移量設置爲0?

任何提示將是很好

+0

作爲一個開始,讓我提供這個。文件寫入默認情況下不是附加模式,這是您必須明確設置的內容。因此,提到「它總是在文件末尾寫入」的部分會讓人相信這個其他代碼,或者你正在告訴它要追加。源代碼和錯誤消息的歡迎。 – demongolem

+0

讓我也分享這個SO鏈接關於檢查一個文件是否已經打開,以便您可以評估情況http://stackoverflow.com/questions/1390592/java-check-if-file-is-already-open – demongolem

+0

其他代碼告訴它附加,是的。雖然我的目標是不修改他們的代碼(因爲他們發佈更新,而且我將不得不始終更改他們的代碼),但我試圖將其設置爲false,並且他們寫入文件的方式無法有效地寫入,除非將其設置爲真正。我正在研究一種擦除文件的方法,可能使用將特定位置的指定位置寫入文件的寫入方法。但是,我不確定我應該怎麼寫?零或空格? @demongolem我不太確定這個鏈接如何是非常有用的,除了告訴我這可能是不可能的? – Sean

回答

0

下面有一些不錯的代碼,可能是有用的你:

public void writeToNewFile(String filePath, String data) 
{ 
    PrintWriter writer; 
    File file; 
    try 
    { 
     file = new File(filePath); 
     file.createNewFile(); 
     writer = new PrintWriter(new FileWriter(file)); 
     writer.println(data); 
     writer.flush(); 
     writer.close(); 
    }catch(Exception e){e.printStackTrace();} 
    writer = null; 
    file = null; 
    \\setting file & writer to null releases all the system resources and allows the files to be accessed again later 
} 

//this will write to end of file 
public void writeToExistingFile(String filePath, String data) 
{ 
    PrintWriter writer; 
    File file; 
    try 
    { 
     file = new File(filePath); 
     if(!file.exists()) 
      file.createNewFile(); 
     writer = new PrintWriter(new FileWriter(file,true)); 
     writer.println(data); 
     writer.flush(); 
     writer.close(); 
    }catch(Exception e){e.printStackTrace();} 
    writer = null; 
    file = null; 
    \\setting file & writer to null releases all the system resources and allows the files to be accessed again later 
} 

public String[] readFile(String filePath) 
{ 
    String data[]; 
    Iterator<String> it; 
    ArrayList<String> dataHolder = new ArrayList<String>(); 
    BufferedReader reader; 
    File file; 
    try 
    { 
     file = new File(filePath); 
     reader = new BufferedReader(new FileReader(file)); 

     int lines = 0; 
     while(reader.ready()) 
     { 
      lines++; 
      dataHolder.add(reader.readLine()); 
     } 

     data = new String[lines]; 
     it = dataHolder.iterator(); 
     for(int x=0;it.hasNext();x++) 
      data[x] = it.next(); 

     reader.close(); 
    }catch(Exception e){e.printStackTrace();} 
    reader = null; 
    file = null; 
    \\setting file & reader to null releases all the system resources and allows the files to be accessed again later 
    return data; 
} 

public void deleteFile(String filePath) 
{ 
    File file; 
    try 
    { 
     file = new File(filePath); 
     file.delete(); 
    }catch(Exception e){e.printStackTrace();} 
    file = null; 
} 

public void createDirectory(String directory) 
{ 
    File directory; 
    try 
    { 
     directory = new File(directory); 
     directoyr.mkDir(); 
    }catch(Exception e){e.printStackTrace();} 
    directory = null; 
} 

希望這有助於!

0

@John Detter,我已經嘗試了很大一部分,雖然這是一些很好/有用的代碼。

我通過在一個單獨的線程中打開文件(當我知道我不是讀取/寫入它時)作爲RandomAccessFile來解決它。我得到了該文件的長度,然後調用raf.skipBytes(長度)並刪除了該文件。 還有其他一些奇怪的事情,但它適用於我。

+1

約翰不會​​得到你的消息。你有它正確的格式,但它需要是一個評論,而不是一個答案。 – Keng

相關問題