如何创建文件?Java代码创建file.txt报错java.io.FileNotFoundException(Read-only file system)咨询
Hey there! Let's tackle your two questions one by one, starting with the error you're facing since that's the immediate roadblock.
1. Why You're Seeing java.io.FileNotFoundException: file.txt (Read-only file system)
The error message says it all: your Java program is trying to create file.txt in a read-only file system—meaning the directory your code is running in doesn't allow write operations. Looking at your stack trace, it looks like you're using an Android-based Java IDE, where the default working directory is often a restricted system folder that doesn't permit file creation.
When you use FileWriter("file.txt") with a relative path, Java defaults to creating the file in the current working directory. If that directory is locked down (like system directories on Android), you'll get this exact exception.
Fixes to Resolve This Error
Here are two reliable ways to fix this:
Option 1: Use a Writable Absolute Path
Instead of relying on the default working directory, explicitly point to a folder you have write access to.
For Android (since your stack trace shows an Android IDE):
Use your app's private external storage directory—this is a safe, writable area for your app's files:package Com.test; import java.io.FileWriter; import java.io.IOException; import java.io.File; public class Main { public static void main(String args[]) throws IOException { // Get the path to your app's private external storage folder String writablePath = System.getenv("EXTERNAL_STORAGE") + "/Android/data/com.test/files/"; File writableDir = new File(writablePath); // Create the directory if it doesn't exist if (!writableDir.exists()) { writableDir.mkdirs(); } // Now create the file in this writable directory File outputFile = new File(writableDir, "file.txt"); FileWriter output = new FileWriter(outputFile); // Always close the writer to avoid resource leaks output.close(); } }For desktop Java environments:
Use your user's home directory, which is always writable:FileWriter output = new FileWriter(System.getProperty("user.home") + "/file.txt");
Option 2: Check Directory Permissions (Desktop/Linux)
If you're running on a Linux/macOS desktop, ensure you have write permissions for the current directory. Run this command in your terminal to grant yourself write access:
chmod u+w .
2. How to Create a File in Java
There are a few common, reliable methods to create files in Java—pick the one that fits your use case:
Method 1: FileWriter (For Text Files)
This is great for writing text content directly to a file. Use try-with-resources (Java 7+) to automatically close the writer and avoid resource leaks:
import java.io.FileWriter; import java.io.IOException; public class CreateTextFile { public static void main(String[] args) { try (FileWriter writer = new FileWriter(System.getProperty("user.home") + "/my-text-file.txt")) { writer.write("Hello, this is my first Java-created file!"); } catch (IOException e) { e.printStackTrace(); } } }
Method 2: java.nio.file.Files (Modern NIO Approach)
This is the recommended method for newer Java versions—it's more flexible and handles file operations cleanly:
import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.StandardCharsets; public class CreateFileNIO { public static void main(String[] args) { try { String fileContent = "Sample content using NIO"; // Create file and write content in one step Files.write(Paths.get(System.getProperty("user.home") + "/nio-file.txt"), fileContent.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); } } }
To create an empty file with NIO:
Files.createFile(Paths.get(System.getProperty("user.home") + "/empty-nio-file.txt"));
Method 3: java.io.File (Create Empty Files)
Use this if you just need to create an empty file first, then write to it later:
import java.io.File; import java.io.IOException; public class CreateEmptyFile { public static void main(String[] args) { File emptyFile = new File(System.getProperty("user.home") + "/empty-file.txt"); try { if (emptyFile.createNewFile()) { System.out.println("Empty file created successfully!"); } else { System.out.println("File already exists."); } } catch (IOException e) { e.printStackTrace(); } } }
内容的提问来源于stack exchange,提问作者Alfredo Alexander Mendez

