2012-07-13 50 views
0
private static boolean replaceUserPassword(String lineToBeReplaced, String replacementLine) {  
    try 
    { 
    File file = new File(defaultPath); 
    Runtime.getRuntime().exec("attrib -H " + file.getCanonicalPath()); 
    System.out.println(file.isHidden()); 

    BufferedReader reader = new BufferedReader(new FileReader(file)); 
    String line = "", oldtext = ""; 
    while((line = reader.readLine()) != null) 
     { 
     oldtext += line + "\r\n"; 
     } 
    reader.close(); 

    //To replaces line in defaultPath file 
    String newtext = oldtext.replaceAll(lineToBeReplaced, replacementLine); 

    file.setWritable(true); 
    if(file.canWrite()){ 
     System.out.println("writing"); 
     FileWriter writer = new FileWriter(defaultPath, false); 
     writer.write(newtext); 
     writer.close(); 
    } 

    Runtime.getRuntime().exec("attrib +H " + file.getCanonicalPath()); 

    } 
catch (IOException ioe) 
    { 
    ioe.printStackTrace(); 
    return false; 
    } 
    return true;   

}無法用Java覆蓋隱藏文件。當我試圖用我的代碼,我得到java.io.FileNotFoundException(訪問被拒絕)

我沒有在文件中的任何其他窗口打開,工藝作品,當我取消隱藏文件放在原始文件夾中。 感謝您的幫助。

回答

0

除非將其設置爲取消隱藏,否則無法獲取隱藏文件的規範路徑。因此是錯誤拋出。

變化:

File file = new File(defaultPath); 
Runtime.getRuntime().exec("attrib -H " + file.getCanonicalPath()); 
System.out.println(file.isHidden()); 

要:

Runtime.getRuntime().exec("attrib -H " + defaultCanonicalPath); 
// OR 
// Runtime.getRuntime().exec("attrib -H " + defaultPath); 
File file = new File(defaultPath); 
System.out.println(file.isHidden()); 

而且,它應該是工作。

+0

嘿我改變了我的代碼,你已經顯示,現在它取消隱藏文件。但是,當嘗試覆蓋文件時,它仍然顯示相同的錯誤。任何想法爲什麼?另外,儘管最初運行代碼時System.out.println(file.isHidden)仍然返回true,但如果再次運行,它將返回false。 – user1524076 2012-07-13 23:32:19

相關問題