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

RandomAccessFile writes to the file which does not has any read,write and execute permissions

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • None
    • generic
    • generic

      Tested in Solaris and Linux for bash,csh and ksh shells.

      RandomAccessFile.writeBytes() writes and reads a file created under umask 777.
      Set umask to 777 , so that any file created will have 000 permissions.

      Read and write methods in java.io should check file permissions of a file before reading and writing (or) File.canRead(),File.canWrite() and File.canExecute() return true, for the situations like this one otherwise it seems to be odd writing to a file for which File.canWrite() returns false.


      <CODE>
      import java.io.*;
      import static java.lang.System.out;

      class Test8 {

        public static void main(String ... args) throws IOException {
          
          byte b[] = new byte[20];
          File f = new File ("raf");
          RandomAccessFile raf = new RandomAccessFile(f,"rw");
          
          out.println("can Read "+f.canRead());
          out.println("can Write "+f.canWrite());
          out.println("can Execute "+f.canExecute());
          
          out.println("Writing Hello ...");
          raf.writeBytes("Hello ");
          out.println("done.");
          
          out.println("Writing World ...");
          raf.writeBytes("World");
          out.println("done.");
          
          out.print("reading ...");
          raf.seek(0);
          raf.read(b);
          out.println(new String(b));
          raf.close();
          
        }
      }
      </CODE>
      <Result>
      bash-3.00$ uname -a
      SunOS client24 5.10 Generic sun4u sparc SUNW,Ultra-60
      bash-3.00$ umask -S
      u=,g=,o=
      bash-3.00$ java Test8
      can Read false
      can Write false
      can Execute false
      Writing Hello ...
      done.
      Writing World ...
      done.
      reading ...Hello World
      bash-3.00$

      </Result>

            Unassigned Unassigned
            rgutupalsunw Rajendra Gutupalli (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: