Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - file.length() returns even if the file is not readable

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

like image 228
una_dinosauria Avatar asked Jan 12 '13 03:01

una_dinosauria


People also ask

What does file length return?

length() returns the length of the file defined by this abstract pathname. The return value is unspecified if this pathname defines a directory.

What will the method length () in the class file return?

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.

How do you make a file not readable in Java?

setReadable(false,false); f1.

How do you check csv file is empty or not in Java?

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.


1 Answers

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.

like image 166
us2012 Avatar answered Oct 23 '22 00:10

us2012