Is it possible to open file without using any imports?

I am currently learning how to work with strings in Java.

I am given file.txt and there are a lot of strings with random chars. And basically I do some stuff with that file to complete a task.

100% of the time I use BufferedReader to open this file. I am just curious, are there any capabilities to open it without using third party imports?

Thanks for the answer.

Answer

Not in the way you’re probably thinking, no.

The BufferedReader class is just one of the many classes within the Java API that can be used to read from files, but, practically speaking, you do need to import and use at least one of these classes if you want to read from a file. In other words, there is no inbuilt capability like a keyword or anything that you can use to read from a file.

The vast majority of the code you will write in Java will be based upon the classes present in the Java API, and you will have to import these classes in order to use them and take advantage of the functionality they provide. Importing the java.io package, for example, will give you access to classes designed for reading from/writing to files, such as the BufferedReader and BufferedWriter classes. Importing the java.util package will allow you to use classes designed for managing collections, such as the ArrayList and Map classes.

To learn more about the many java packages and classes that are at your disposal, you can find a full list of them here.

EDIT: As Pshemo mentioned in their comment on your post, you don’t have to explicitly use an import statement to gain access to a class from the java API. You can always use the fully qualified name of the class if you’d prefer (like java.io.BufferedReader, for example), but makes your code more verbose than necessary and ultimately harder to read as well.

Leave a Reply

Your email address will not be published. Required fields are marked *