• Post Reply Bookmark Topic Watch Topic
  • New Topic

File handle

 
jason williams
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When reading wikipedia, it says that file handle is equivalent to file descriptor. So I think in Java it means FileDescriptor. However, it also points that in C, it is `FILE *' This seems to me it is File object in Java. So I am confused. What is equivalent in Java for `file handle'?


Thanks.
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of a file descriptor as an integer. Technically, it's an index into a kernel data table of currently opened files, but even to a C programmer it's just a return value from certain system calls, which might need to be passed as a parameter to other system calls. A file handle is a higher level data structure in C, but it probably contains a file descriptor in it. To be fair, there's some muddling of the terms file descriptor and file handle in different operating systems and programming languages. In Java, we work at a higher level even than file handles.


In any case, while file handles and file descriptors are relevant to open files, Java's File object just provide certain operations on file system files, without necessarily opening them. You can't write to a file or read from it through the File methods, so there may not be a file handle even created for it in the underlying OS. Now if your File is a directory entry, and you call, say, listFiles() on it, the OS may get a file handle to it, then use that to read the list of files. However, it would then close the file and dispose of the file handle.
 
jason williams
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:I think of a file descriptor as an integer. Technically, it's an index into a kernel data table of currently opened files, but even to a C programmer it's just a return value from certain system calls, which might need to be passed as a parameter to other system calls. A file handle is a higher level data structure in C, but it probably contains a file descriptor in it. To be fair, there's some muddling of the terms file descriptor and file handle in different operating systems and programming languages. In Java, we work at a higher level even than file handles.


In any case, while file handles and file descriptors are relevant to open files, Java's File object just provide certain operations on file system files, without necessarily opening them. You can't write to a file or read from it through the File methods, so there may not be a file handle even created for it in the underlying OS. Now if your File is a directory entry, and you call, say, listFiles() on it, the OS may get a file handle to it, then use that to read the list of files. However, it would then close the file and dispose of the file handle.


Thanks for the explain. It's helpful!
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!