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;        

}

我没有在任何其他窗口中打开该文件,当我取消隐藏原始文件夹中的文件时,该过程有效。感谢您对未来的任何帮助。

4

1 回答 1

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() );

而且,它应该可以工作。

于 2012-07-13T17:26:01.157 回答