In this tutorial, we will learn how to create a new file in Java.
The java.io.File class can be used to create a new file in Java. When we initialize the File object, we provide the file name, and then we can call createNewFile() method to create a new file in Java.
File createNewFile() method returns true if a new file is created and false if the file already exists. This method also throws java.io.IOException when it’s not able to create the file.
Create a New File Example
- Create File class object by passing file absolute location path "C://workspace/sample.txt"
- call createNewFile() method of the File object to create a new file named "sample.txt" file in the directory "C://workspace"
- The file createNewFile() method returns true if a new file is created and false if the file already exists. This method also throws java.io.IOException when it’s not able to create the file.
- Observe the directory whether the file is created or not.
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This Java program demonstrates how to create new file in Java.
* @author javaguides.net
*/
public class CreateFileExample {
private static final Logger LOGGER = LoggerFactory
.getLogger(CreateFileExample.class);
public static void main(String[] args) {
createFile();
}
public static void createFile() {
File file = new File("C:/workspace/sample.txt");
try {
if (file.createNewFile()) {
LOGGER.info("File is created !!");
} else {
LOGGER.info("File is already exist");
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
}
Create a File using FileOutputStream
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
public class WriteFile {
/**
* This class shows how to write file in java
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String data = "I will write this String to File in Java";
int noOfLines = 10000;
// Use Streams when you are dealing with raw data
try(OutputStream os = new FileOutputStream(new File("C:/workspace/sample.txt"))){
os.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
public class WriteFile {
/**
* This class shows how to write file in java
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String data = "I will write this String to File in Java";
int noOfLines = 10000;
// Use Streams when you are dealing with raw data
try(OutputStream os = new FileOutputStream(new File("C:/workspace/sample.txt"))){
os.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reference
File Handling Examples
- How to Create a New File in Java
- How to Write to File in Java
- How to delete a file in Java
- How to Copy the File in Java
- How to Move a File in Java
- How to Rename the File in Java
- How to Append to File in Java
- Read the File using BufferReader
- Read File using BufferInputStream
- Read the File using DataInputStream
- Reading a File in Java using BufferedInputStream
- Read a File in Java using BufferedReader
- Java Get File Extension From Filename
- Get File Size in Bytes KB MB GB TB
- How to Check if a File is Empty in Java
Comments
Post a Comment
Leave Comment