I have a file from which I have suppressed every permission. No one should be able to read the file, right? In fact, if I run
File f = new File("not_readable.pdf");
System.out.println(f.canRead())
I get
false
However, if I call
File f = new File("not_readable.pdf");
System.out.println(f.length())
I get
455074
It is my understanding that in order to get the size of a file one must open and read the file first, but this result strongly suggests that I'm wrong. Does anyone know why this happens? Also, is there a way to prevent Java's file.length() method from accessing the size of a file?
I am using Ubuntu 12.10
length() returns the length of the file defined by this abstract pathname. The return value is unspecified if this pathname defines a directory.
The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length. The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs.
setReadable(false,false); f1.
Well, it's pretty easy to check emptiness for a file in Java by using the length() method of the java. io. File class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist.
You are mistaken: The length of a file is file system metadata (at least for file systems running under the Linux VFS). Anyone who has read permissions to a directory can see all the contained files and their sizes. To prevent users from seeing a file's size, you have to prevent them from seeing it altogether, i.e. place the file in a directory that has permissions like drwxr-x---
if the user is not in the group associated to the directory, or drwx------
if the user is in that group.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With