Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8047342

FileOutputStream fails with Access denied when file has "hidden" or "system" attributes set

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P5 P5
    • tbd
    • 8u5
    • core-libs
    • x86
    • windows_2003

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Java is unable to write to files which have either or both of the Windows "hidden" or "system" file attributes set, and throws an exception when this is attempted.

      Such files are not considered read-only by other Windows programs or by the underlying operating system. In other programs these files may be written freely (if you know where they are), without needing to recreate the file from scratch, and while preserving the attributes.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.FileNotFoundException: <filename> (Access is denied)
              at java.io.FileOutputStream.open(Native Method)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:206)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:156)
              at java.io.FileWriter.<init>(FileWriter.java:90)
              at HiddenFileWritingBug.main(HiddenFileWritingBug.java:25)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.nio.file.Files;

      /**
       * Demonstration of a Java bug with hidden and system files. The program generates a name
       * for a temporary file and writes to it successfully. Then it marks it hidden and system.
       * Thereafter, it is unable to write to the file any more, getting a FileNotFoundException
       * on the second FileWriter creation call.
       */
      class HiddenFileWritingBug {
      public static void main(String... args) throws Throwable {
      File file = File.createTempFile("foo", "bar");

      try (Writer out = new FileWriter(file)) {
      out.write("1");
      System.out.println("1 OK");
      } catch (IOException e) {
      System.out.println("1 failed");
      e.printStackTrace();
      }

      java.nio.file.Files.setAttribute(file.toPath(), "dos:hidden", true);
      java.nio.file.Files.setAttribute(file.toPath(), "dos:system", true);

      try (Writer out = new FileWriter(file)) {
      out.write("2");
      System.out.println("2 OK");
      } catch (IOException e) {
      System.out.println("2 failed");
      e.printStackTrace();
      }

      file.delete(); // cleanup
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If writing to a file fails, delete it and try again.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: