Create New file


There are 3 ways to create a new file in Java

1) Using java.io.File.createNewFile() method

2) Using java.io.FileOutputStream class

3) Using java.nio.file.Files from NIO API



1) Using java.io.File.createNewFile() method

  1. File file = new File("C:\\newfile1.txt");
  2.  
  3. //Creates new file and return true if file not exists and return false if file already exists
  4. boolean fvar = file.createNewFile();
  5. if (fvar){
  6.               System.out.println("File has been created successfully");
  7.          }
  8.          else{
  9.               System.out.println("File already present at the specified location");
  10.          }
  11.         }
File file = new File("C:\\newfile1.txt");

//Creates new file and return true if file not exists and return false if file already exists
boolean fvar = file.createNewFile(); 
if (fvar){
	          System.out.println("File has been created successfully");
	     }
	     else{
	          System.out.println("File already present at the specified location");
	     }
        }


2) Using java.io.FileOutputStream class

FileOutputStream.write() method automatically create a new file and write content to it.

  1. String data = "Welcome to File creation";
  2. FileOutputStream out = new FileOutputStream("c:// newfile2.txt");
  3. out.write(data.getBytes());
  4. out.close();
String data = "Welcome to File creation";
FileOutputStream out = new FileOutputStream("c:// newfile2.txt");
out.write(data.getBytes());
out.close();


3) Using java.nio.file.Files from NIO API

  1. String data = " Welcome to File creation ";
  2. Files.write(Paths.get("c: //newfile3.txt"), data.getBytes());
  3.  
  4. //or
  5.  
  6. List<String> lines = Arrays.asList("line 1", "line 2");
  7. Files.write(Paths.get("newfile4.txt"), lines, StandardCharsets.UTF_8,
  8.         StandardOpenOption.CREATE, StandardOpenOption.APPEND);
String data = " Welcome to File creation ";
Files.write(Paths.get("c: //newfile3.txt"), data.getBytes());
 
//or
 
List<String> lines = Arrays.asList("line 1", "line 2");
Files.write(Paths.get("newfile4.txt"), lines, StandardCharsets.UTF_8,
        StandardOpenOption.CREATE, StandardOpenOption.APPEND);


Complete example is as below

  1. package com.kb.io;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.nio.charset.StandardCharsets;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.util.Arrays;
  12. import java.util.List;
  13.  
  14. public class CreateNewFile
  15. {
  16.     public static void main(String[] args) throws IOException
  17.     {
  18.         createFileUsingFileClass();
  19.         createFileUsingFileOutputStreamClass();
  20.         createFileUsingNIO();
  21.     }
  22.  
  23.     private static void createFileUsingFileClass() throws IOException
  24.     {
  25.     File file = new File("C://newfile1.txt");
  26.  
  27. //Creates new file and return true if file not exists and return false if file already exists
  28. boolean isFileCreated = file.createNewFile();
  29. if (isFileCreated){
  30.               System.out.println("File has been created successfully");
  31.          }
  32.          else{
  33.               System.out.println("File already present at the specified location");
  34.          }
  35.  
  36.            
  37.           //Write Content
  38.           FileWriter writer = new FileWriter(file);
  39.           writer.write("File Creation using File class");
  40.           writer.close();
  41.     }
  42.  
  43.     private static void createFileUsingFileOutputStreamClass() throws IOException
  44.     {
  45.         String data = "File Creation using FileOutputStream class";
  46.         FileOutputStream out = new FileOutputStream("c://newfile2.txt");
  47.         out.write(data.getBytes());
  48.         out.close();
  49.     }
  50.  
  51.     private static void createFileUsingNIO()  throws IOException
  52.     {
  53.         String data = "File Creation using NIO API";
  54.         Files.write(Paths.get("c://newfile3.txt"), data.getBytes());
  55.          
  56.         //This way it creates new file if no file exist with the given name or appends if already exists
  57.          
  58.         List<String> lines = Arrays.asList("line1", "line2",”line3”);
  59.         Files.write(Paths.get("c://newfile4.txt"), lines, StandardCharsets.UTF_8,
  60.                 StandardOpenOption.CREATE, StandardOpenOption.APPEND);
  61.     }
  62. }
package com.kb.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
 
public class CreateNewFile
{
    public static void main(String[] args) throws IOException
    {
        createFileUsingFileClass();
        createFileUsingFileOutputStreamClass();
        createFileUsingNIO();
    }
 
    private static void createFileUsingFileClass() throws IOException
    {
	File file = new File("C://newfile1.txt");

//Creates new file and return true if file not exists and return false if file already exists
boolean isFileCreated = file.createNewFile(); 
if (isFileCreated){
	          System.out.println("File has been created successfully");
	     }
	     else{
	          System.out.println("File already present at the specified location");
	     }

           
          //Write Content
          FileWriter writer = new FileWriter(file);
          writer.write("File Creation using File class");
          writer.close();
    }
 
    private static void createFileUsingFileOutputStreamClass() throws IOException
    {
        String data = "File Creation using FileOutputStream class";
        FileOutputStream out = new FileOutputStream("c://newfile2.txt");
        out.write(data.getBytes());
        out.close();
    }
 
    private static void createFileUsingNIO()  throws IOException
    {
        String data = "File Creation using NIO API";
        Files.write(Paths.get("c://newfile3.txt"), data.getBytes());
         
        //This way it creates new file if no file exist with the given name or appends if already exists
         
        List<String> lines = Arrays.asList("line1", "line2",”line3”);
        Files.write(Paths.get("c://newfile4.txt"), lines, StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    }
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on