Can a File Object Alone Read a File?

  • Thread starter Thread starter Punkyc7
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
A File object in Java cannot read a file on its own, as it lacks methods for reading or writing data. To access the contents of a file, additional classes such as Scanner or BufferedReader must be utilized. The File class primarily serves to represent file and directory pathnames rather than handle file I/O operations. For effective file reading, one must instantiate these additional classes alongside the File object. Therefore, a File object alone is insufficient for reading file data.
Punkyc7
Messages
415
Reaction score
0
Can a File object alone be used to read a file?

I am thinking it is no because don't you have to use the scanner class to get the data from the file?

I have tried looking for this online but haven't been able to find the answer.
 
Physics news on Phys.org
I should probably add this is in java
 
Punkyc7 said:
Can a File object alone be used to read a file?

I am thinking it is no because don't you have to use the scanner class to get the data from the file?

I have tried looking for this online but haven't been able to find the answer.

Punkyc7 said:
I should probably add this is in java
Yes, that would be good to do.

The File class has no methods for reading (or writing to) a File instance. For the methods on the File class, see http://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html.
 
Back
Top