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
2
3
4
5
6
7
8
9
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");
         }
        }
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
2
3
4
String data = "Welcome to File creation";
FileOutputStream out = new FileOutputStream("c:// newfile2.txt");
out.write(data.getBytes());
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
2
3
4
5
6
7
8
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);
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
    }
}
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

Karibasappa G C (KB)
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