-1

hello guys I want to give file permission to open in read mode or write mode .ext contains file extention and file_name contains name of file. f_p is a veriable where I an geting input as 'r' or 'w' mode. Here I am using same file at different locations

But in this code I am getting error as cannot find symbol: method setReadable(boolean) location: fos2 is of type FileOutputStream

<%

some code here

FileInputStream fis2 = new FileInputStream("e:/profile/epy/"+file_name+".ext");

        FileOutputStream fos2 = new FileOutputStream("e:/decrypt/"+file_name+"."+ext);

                    if(f_p.equals("R")||f_p.equals("r"))
                    {
                        fos2.setReadable(true);
                    }
                    else if(f_p.equals("W")||f_p.equals("w"))
                    {
                        fos2.setWritable(true);
                    }

// some code here

%> https://jsfiddle.net/wc8pccyL/

  • 1
    Yep, the [Javadocs](https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html) clearly show that `FileOutputStream` does not have a .setReadable() method. The File object, OTOH, does. So use `File` not `FileOutputStream`, and make friends with the Javadocs. – KevinO Apr 14 '16 at 17:41

1 Answers1

0

The current code uses the wrong class (FileOutputStream).

File f = new File(SOME_PATH);
if ("r".equalsIgnoreCase(f_p)) {
   f.setReadable(true);
   ...
}
if ("w".equalsIgnoreCase(f_p)) {
   f.setWritable(true);
   ...
}

However, one should be careful in assuming that one would want write access without read access. The assumption in the OP's code is that the f_p has a single value of "R" or "W", and sets the permission. This assumption should be carefully checked, especially across operating systems.

Also, if the FileOutputStream has to be used later (for actual output), it has a constructor that takes a File object, so there is nothing lost by creating the File object in such a scenario, and then creating the FileOutputStream fos = new FileOutputStream(f); where 'f' is the previously instantiated File object.

KevinO
  • 4,303
  • 4
  • 27
  • 36