Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法覆盖Java中的隐藏文件。当我尝试使用我的代码时,我得到java.io.FileNotFoundException(访问被拒绝)_Java_File_Java Io - Fatal编程技术网

无法覆盖Java中的隐藏文件。当我尝试使用我的代码时,我得到java.io.FileNotFoundException(访问被拒绝)

无法覆盖Java中的隐藏文件。当我尝试使用我的代码时,我得到java.io.FileNotFoundException(访问被拒绝),java,file,java-io,Java,File,Java Io,} 我没有在任何其他窗口中打开该文件,并且当我在原始文件夹中取消隐藏该文件时,该过程会起作用。 谢谢你以后的帮助 除非设置为取消隐藏,否则无法获取隐藏文件的规范路径。因此会抛出错误 更改: private static boolean replaceUserPassword(String lineToBeReplaced, String replacementLine) { try { File file = new File(defaultPath);

}

我没有在任何其他窗口中打开该文件,并且当我在原始文件夹中取消隐藏该文件时,该过程会起作用。
谢谢你以后的帮助

除非设置为取消隐藏,否则无法获取隐藏文件的规范路径。因此会抛出错误

更改:

  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;        
致:

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

而且,它应该是有效的

嘿,我更改了你显示的代码,现在它取消隐藏文件。但是,当试图覆盖文件时,它仍然显示相同的错误。你知道为什么吗?此外,System.out.println(file.ishiden)在最初运行代码时仍然返回true,但如果再次运行,则返回false。
Runtime.getRuntime().exec( "attrib -H " + defaultCanonicalPath );
// OR
// Runtime.getRuntime().exec( "attrib -H " + defaultPath );
File file = new File( defaultPath );
System.out.println( file.isHidden() );