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
- 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.
- 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
- 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
- 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); } }