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 に答える