🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › File.html
File (Java SE 11 & JDK 11 )
January 20, 2026 - Note that the Files class defines the newDirectoryStream method to open a directory and iterate over the names of the files in the directory.
🌐
Baeldung
baeldung.com › home › java › java io › how to read a file in java
How to Read a File in Java | Baeldung
January 8, 2024 - Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence. Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time. ... In this tutorial, we’ll explore different ways to read from a File in Java.
🌐
How-To Geek
howtogeek.com › home › windows › how to open jar files on windows 10 and 11
How to Open JAR Files on Windows 10 and 11
September 23, 2023 - To run a JAR file on Windows 10 or Windows 11, right-click it, then select Open With > Java Platform SE Binary.
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › io › File.java
openjdk-jdk11/src/java.base/share/classes/java/io/File.java at master · AdoptOpenJDK/openjdk-jdk11
March 2, 2019 - * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method · * to open a directory and iterate over the names of the files in the · * directory. This may use less resources when working with very large · * directories. * * @return An array of abstract pathnames denoting the files and ·
Author   AdoptOpenJDK
🌐
Super User
superuser.com › questions › 1818089 › how-to-set-default-way-of-opening-java-file-in-win11
windows 11 - how to set default way of opening *.java file in win11 - Super User
November 25, 2023 - I have also tried to change regedit table according to this note, but I can't find the directory https://blog.csdn.net/weixin_34379040/article/details/114218801 ... Settings -> Apps -> Default apps. Then type .java in the text field under "Set ...
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - If you want to read bytes from a file (and in older Java versions < 11 you’d have to use the same API for reading strings), you need to call Files.readAllBytes.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › FileSystem.html
FileSystem (Java SE 11 & JDK 11 )
October 20, 2025 - A file system is open upon creation and can be closed by invoking its close method. Once closed, any further attempt to access objects in the file system cause ClosedFileSystemException to be thrown.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › › java.base › java › nio › file › package-summary.html
java.nio.file (Java SE 11 & JDK 11 )
October 20, 2025 - The view may or may not, however, be consistent with the view of the file system as seen by other concurrently running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified. The SYNC and DSYNC options are used when opening a file to require that updates to the file are written synchronously to the underlying storage device.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › Files.html
Files (Java SE 11 & JDK 11 )
January 20, 2026 - Tests whether a file is readable. This method checks that a file exists and that this Java virtual machine has appropriate privileges that would allow it open the file for reading. Depending on the implementation, this method may require to read file permissions, access control lists, or other ...
🌐
IBM
ibm.com › docs › en › app-connect › 11.0.0
Opening an existing Java file
You can add to and modify Java code that you have created in a Java project.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › OpenOption.html
OpenOption (Java SE 11 & JDK 11 )
January 20, 2026 - Objects of this type are used by methods such as newOutputStream, newByteChannel, FileChannel.open, and AsynchronousFileChannel.open when opening or creating a file.
🌐
JetBrains
jetbrains.com › guide › java › tips › open-file
Open File - JetBrains Guide
March 22, 2024 - Open a file from the Project tool window by selecting the file and pressing ⏎ (macOS) / Enter (Windows/Linux). Or open the selected file in a new window by pressing ⇧⏎ (macOS) / Shift+Enter (Windows/Linux) · Copyright © 2000–2026 JetBrains ...
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › file.html
Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Java Classes > Basic I/O)
CREATE_NEW – Creates a new file and throws an exception if the file already exists. CREATE – Opens the file if it exists or creates a new file if it does not.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-open-file
How to open a File in Java | DigitalOcean
August 4, 2022 - Sometimes we have to open a file in java program. java.awt.Desktop can be used to open a file in java. Desktop implementation is platform dependent, so first, we should check if the operating system supports Desktop or not.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 4141935 › open-with-file
Open with file - Microsoft Q&A
June 20, 2023 - 2023-06-21T04:11:20+00:00 · Hi, Joshua. I'm Robinson, and I’m happy to help you today. You will need to go to the folder where Java is installed and select "Javaw.exe". 1-Right-click on the .jar file and select Open with> choose another app ·
Top answer
1 of 16
765

My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases):

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}

Some has pointed out that after Java 7 you should use try-with-resources (i.e. auto close) features:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
}

When I read strings like this, I usually want to do some string handling per line anyways, so then I go for this implementation.

Though if I want to actually just read a file into a String, I always use Apache Commons IO with the class IOUtils.toString() method. You can have a look at the source here:

http://www.docjar.com/html/api/org/apache/commons/io/IOUtils.java.html

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
    String everything = IOUtils.toString(inputStream);
} finally {
    inputStream.close();
}

And even simpler with Java 7:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {     
    String everything = IOUtils.toString(inputStream);
    // do something with everything string
}
2 of 16
648

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

Go through this article on how to use a Reader

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

In Java 7:

new String(Files.readAllBytes(...))

(docs) or

Files.readAllLines(...)

(docs)

In Java 8:

Files.lines(..).forEach(...)

(docs)

🌐
W3Schools
w3schools.com › java › java_files_read.asp
Java Read Files
Explanation: This program opens the file named filename.txt and reads it line by line using a Scanner. Each line is printed to the console. If the file cannot be found, the program will print "An error occurred." instead. To get more information about a file, use any of the File methods: import java.io.File; // Import the File class public class GetFileInfo { public static void main(String[] args) { File myObj = new File("filename.txt"); if (myObj.exists()) { System.out.println("File name: " + myObj.getName()); System.out.println("Absolute path: " + myObj.getAbsolutePath()); System.out.println("Writeable: " + myObj.canWrite()); System.out.println("Readable " + myObj.canRead()); System.out.println("File size in bytes " + myObj.length()); } else { System.out.println("The file does not exist."); } } }
🌐
Oracle
docs.oracle.com › en › java › javase › › 11 › docs › api › java.base › java › io › FileInputStream.html
FileInputStream (Java SE 11 & JDK 11 )
January 20, 2026 - Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system. A new FileDescriptor object is created to represent this file connection.