Java: Read a File With No Extension
In this article
Reading a file that has no extension in Java is exactly the same as reading any other file—the absence of an extension does not affect how Java's I/O classes locate or read the file. You simply provide the file's path (or name) as you would for a file with an extension. This guide shows you the most common, reliable ways to do it, using both older and newer Java I/O APIs.
What You'll Need
- A Java Development Kit (JDK) installed (version 8 or later recommended; the examples use APIs available since Java 8).
- A text editor or IDE (like IntelliJ, Eclipse, or VS Code).
- Access to the file you want to read—know its path (absolute or relative to your project).
- Basic familiarity with Java syntax and exception handling.
Step-by-Step
- Locate the file path. Determine the exact path to the file. For example, on Windows:
C:\data\config; on Linux/macOS:/home/user/data/config. If the file is in your project's root, you can use a relative path likeconfig(no extension). - Choose an I/O method. Java offers several ways to read a file. For simplicity and modern style, use
Files.readAllLines()orFiles.readString()(Java 11+). For older versions, useBufferedReaderwithFileReader. We'll cover both. - Method 1: Using Files.readAllLines (Java 8+). This reads all lines into a
List<String>in one go. Example:import java.nio.file.*; import java.util.List; public class ReadNoExt { public static void main(String[] args) { Path filePath = Paths.get("config"); // no extension try { List<String> lines = Files.readAllLines(filePath); for (String line : lines) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } - Method 2: Using Files.readString (Java 11+). If the file is text and you want the whole content as a single string:
import java.nio.file.*; public class ReadNoExtString { public static void main(String[] args) { Path filePath = Paths.get("data"); try { String content = Files.readString(filePath); System.out.println(content); } catch (IOException e) { e.printStackTrace(); } } } - Method 3: Using BufferedReader (works in all Java versions). This is useful for large files or when you need to process line by line without loading everything into memory:
import java.io.*; public class ReadNoExtBuffered { public static void main(String[] args) { File file = new File("config"); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } - Handle exceptions. All file I/O methods throw
IOException(or its subclasses). You must catch it, or declare it in the method signature withthrows IOException. - Run your program. Compile and run it. Ensure the file is in the expected location relative to the working directory of the JVM (often the project root). If not, adjust the path.
Common Mistakes
- Assuming the file must have an extension. Java does not care about extensions; it reads the file based on the path you provide. Do not try to append an extension to the path.
- Using the wrong path. If you use a relative path, it is resolved against the current working directory, which may not be where you think it is. Use an absolute path or check your IDE's working directory settings.
- Forgetting to close resources. In older code, forgetting to close a
FileReaderorBufferedReadercan cause resource leaks. Use try-with-resources (as shown in Method 3) or close in a finally block. - Assuming the file is UTF-8 encoded. If the file uses a different encoding,
Files.readStringandFiles.readAllLinesassume UTF-8 by default. Specify the charset if needed (e.g.,Files.readAllLines(path, StandardCharsets.ISO_8859_1)).
Tips
- Use absolute paths in production code to avoid ambiguity. For example,
Paths.get("/home/user/data/config"). - Check if the file exists before reading to avoid confusing exceptions. Use
Files.exists(path)orfile.exists(). - For binary files with no extension, use
Files.readAllBytes()orInputStreamto read raw bytes. The same principles apply—no extension needed. - Use
Files.newBufferedReader(path)to get aBufferedReaderwith default charset, which is cleaner than manually creating aFileReader. - If you are reading a file that may not be a text file, check the first few bytes for a BOM or magic number to determine its type, but that is beyond basic reading.
Troubleshooting
- FileNotFoundException: The file does not exist at the given path. Double-check the path spelling, case sensitivity, and whether the file is actually in the working directory. Print
System.getProperty("user.dir")to see the current working directory. - NoSuchFileException (a subclass of IOException): Same as above; the path is wrong or the file is missing.
- AccessDeniedException: You do not have permission to read the file. Check file permissions (e.g.,
chmodon Unix or file properties on Windows). - MalformedInputException: The file's bytes do not match the expected charset. Specify the correct charset, for example
StandardCharsets.UTF_16if the file is UTF-16. - OutOfMemoryError when reading a huge file: If you use
readAllLinesorreadStringon a very large file, you may run out of memory. UseBufferedReaderand process line by line. - File is actually a directory: If the path points to a folder,
Files.readAllLineswill throw anIOException(likeIs a directory). Check withFiles.isRegularFile(path).
Frequently Asked Questions
Can I read a file with no extension using the same code as a file with an extension?
Yes, absolutely. The extension is irrelevant to Java's file I/O. You just pass the file's name or path as a string.
What if I don't know the file's extension, but I know its content is text?
You don't need to know the extension. Just read it as text using one of the methods above. If the encoding is unknown, you might need to detect it or try common charsets.
How do I read a binary file with no extension?
Use Files.readAllBytes(Path) to get a byte[], or use an InputStream to read chunks. The lack of an extension does not affect binary reading.
Can I use Scanner to read a file with no extension?
Yes. Scanner works with any file path. For example: Scanner sc = new Scanner(Paths.get("config"));.
What is the default character encoding used by Files.readAllLines?
It uses UTF-8 by default. If your file is not UTF-8, you must pass a Charset as the second argument.
Why do I get a FileNotFoundException even though the file exists?
This usually means the path is incorrect relative to the current working directory. Use an absolute path, or check the working directory with System.getProperty("user.dir").