🌐
W3Schools
w3schools.com › java › java_files_create.asp
Java Create Files
In Java, you can create a new file with the createNewFile() method from the File class.
🌐
W3Schools
w3schools.com › java › java_files.asp
Java Files
Java has several methods for creating, reading, updating, and deleting files. The File class from the java.io package, allows us to work with files.
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
If you are just starting with Java, the easiest way to write text to a file is by using the FileWriter class. In the example below, we use FileWriter together with its write() method to create and write some text into a file.
🌐
YouTube
youtube.com › watch
Create and write to Files in Java - Java tutorial - w3Schools - Chapter-52 English - YouTube
To create a file in Java, we can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if...
Published   July 31, 2022
🌐
W3Schools
w3schools.am › java › java_files_create.html
Java Create and Write To Files
abstract boolean break byte case ... throw throws try void while Java String Methods Java Math Methods ... To create a file in Java, you can use the createNewFile() method....
🌐
W3Schools
w3schools.com › java › exercise.asp
Exercise: - JAVA Create and Write to Files
Delete Files3 q · ArrayList6 q · LinkedList4 q · List Sorting3 q · HashSet4 q · HashMap4 q · Iterator4 q · Wrapper Classes4 q · Regular Expressions5 q · Threads3 q · Lambda Expressions4 q · Advanced Sorting3 q by w3schools.com · Next Question » · Try Again · You have already completed these exercises! Do you want to take them again? Yes No · × · Close the exercise · You completed the JAVA Create and Write to Files Exercises from W3Schools.com ·
🌐
Mkyong
mkyong.com › home › java › java create and write to a file
Java create and write to a file - Mkyong.com
October 2, 2020 - 3.2 In Java 8, we can use the Files.newBufferedWriter to directly create a BufferedWriter object. // default utf_8 try (BufferedWriter bw = Files.newBufferedWriter(path)) { bw.write(content); bw.newLine(); } // append mode try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { bw.write(content); bw.newLine(); } P.S There is nothing wrong with the above FileWriter + BufferedWriter method to write a file, just the Files.write provides more clean and easy to use API.
🌐
W3Schools
w3schools.com › java › java_fileoutputstream.asp
Java FileOutputStream
Here, it is paired with FileOutputStream to write bytes. Together, they make it possible to copy files. By default, FileOutputStream overwrites the file if it already exists. To add (append) new content instead, pass true as the second argument: import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { String text = "\nAppended text!"; // true = append mode (keeps existing content) try (FileOutputStream output = new FileOutputStream("filename.txt", true)) { output.write(text.getBytes()); System.out.println("Successfully appended to file."); } catch (IOException e) { System.out.println("Error writing file."); e.printStackTrace(); } } }
🌐
Programiz
programiz.com › java-programming › examples › create-and-write-to-file
Java Program to Create File and Write to the File
In this example, we will learn to create files in Java and write some information to the file.
Find elsewhere
Top answer
1 of 16
1877

Note that each of the code samples below may throw IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling.

Note that each of the code samples below will overwrite the file if it already exists

Creating a text file:

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();

Creating a binary file:

byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
out.write(data);
out.close();

Java 7+ users can use the Files class to write to files:

Creating a text file:

List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, StandardCharsets.UTF_8);
//Files.write(file, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);

Creating a binary file:

byte data[] = ...
Path file = Paths.get("the-file-name");
Files.write(file, data);
//Files.write(file, data, StandardOpenOption.APPEND);
2 of 16
452

In Java 7 and up:

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("filename.txt"), "utf-8"))) {
   writer.write("something");
}

There are useful utilities for that though:

  • FileUtils.writeStringtoFile(..) from commons-io
  • Files.write(..) from guava

Note also that you can use a FileWriter, but it uses the default encoding, which is often a bad idea - it's best to specify the encoding explicitly.

Below is the original, prior-to-Java 7 answer


Writer writer = null;

try {
    writer = new BufferedWriter(new OutputStreamWriter(
          new FileOutputStream("filename.txt"), "utf-8"));
    writer.write("Something");
} catch (IOException ex) {
    // Report
} finally {
   try {writer.close();} catch (Exception ex) {/*ignore*/}
}

See also: Reading, Writing, and Creating Files (includes NIO2).

🌐
W3Schools
w3schoolsua.github.io › java › java_files_create_en.html
Java Create and Write To Files. Lessons for beginners. W3Schools in English
To create a file in Java, you can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if the file already exists.
🌐
Vultr Docs
docs.vultr.com › java › examples › create-file-and-write-to-the-file
Java Program to Create File and Write to the File | Vultr Docs
December 20, 2024 - Import necessary classes from the java.io package. Create FileWriter and BufferedWriter objects, passing the file path to write contents.
🌐
Javatpoint
javatpoint.com › how-to-create-a-file-in-java
How to Create a File in Java - Javatpoint
How to Create a File in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
🌐
Netlify
w3schools.netlify.app › learnjava › java_files
Java Files
Java has several methods for creating, reading, updating, and deleting files. The File class from the java.io package, allows us to work with files.
🌐
Tutorialspoint
tutorialspoint.com › java › java_create_file.htm
Java - Creating Files
package com.tutorialspoint; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileTest { public static void main(String args[]) { try { File file = new File("d://test//testFile1.txt"); //Create the file if (file.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } // Write Content FileWriter writer = new FileWriter(file); writer.write("Test data"); writer.close(); // read content FileReader reader = new FileReader(file); int c; while ((c = reader.read()) != -1) { char ch = (char) c; System.out.print(ch); } } catch (IOException e) { System.out.print("Exception"); } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-write-into-a-file
Java Program to Write into a File - GeeksforGeeks
July 23, 2025 - // Java Program to Write into a ... block to check if exception occurs try { // Step 1: Create an object of FileOutputStream outputStream = new FileOutputStream("file.txt"); // Step 2: Store byte content from string byte[] ...
🌐
DataCamp
datacamp.com › doc › java › create-&-write-files
Java Create & Write Files
Learn how to efficiently create and write files in Java using classes like File, FileWriter, and BufferedWriter. Follow best practices for error handling and resource management.
🌐
CodeSignal
codesignal.com › learn › courses › fundamentals-of-text-data-manipulation-in-java-1 › lessons › writing-and-appending-text-files-in-java-1
Writing and Appending Text Files in Java
When executed, this sequence of operations will create a file named output.txt (if it doesn't exist), write the specified lines of text into it, and overwrite the content if the file already exists. The content of the file after executing Files.write will look like this: ... Sometimes, you may want to add data to an existing file without overwriting its current contents. This can be easily achieved in Java using the Files.write method with the StandardOpenOption.APPEND flag.
🌐
TechVidvan
techvidvan.com › tutorials › create-open-delete-file-in-java
Java - Create file, Open File and Delete File - TechVidvan
June 17, 2020 - * ; public class FileOpen6 { public static List < String > readFileInList(String fileName) { List < String > lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8); } catch(IOException e) { e.printStackTrace(); } return lines; } public static void main(String[] args) { System.out.println("File content:"); List l = readFileInList("D:\\TechVidvan.txt"); Iterator < String > itr = l.iterator(); //access the elements while (itr.hasNext()) //returns true if and only if scanner has another token System.out.println(itr.next()); //prints the conte
🌐
How to do in Java
howtodoinjava.com › home › i/o › creating a new file in java
Creating a New File in Java
April 10, 2022 - String TEXT_FILE = "C:/temp/io/textFile.txt"; org.apache.commons.io.FileUtils.touch(new File(TEXT_FILE)); Happy Learning !! ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java ...