🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to create, write, read and remove files using Java NIO package APIs.
Java Create File Example
We use Files.createFile() method to create a file in Java.import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.HashSet;
import java.util.Set;
public class JavaCreateFile {
public static void main(String[] args) throws IOException {
Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.OTHERS_READ);
FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
Path myPath = Paths.get("src/resources/sample.txt");
if (Files.exists(myPath)) {
System.out.println("File already exists");
} else {
Files.createFile(myPath, attrs);
System.out.println("File created");
}
}
}Create a new file named "sample.txt" if the file does not exist:
if (Files.exists(myPath)) {
System.out.println("File already exists");
} else {
Files.createFile(myPath, attrs);
System.out.println("File created");
}Java Write File Example
We use Files.write() method to write a text or string to the given file.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
public class JavaWriteFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/sample.txt");
List<String> lines = new ArrayList<>();
lines.add("mango");
lines.add("apple");
lines.add("banana");
lines.add("watermelon");
lines.add("orange");
Files.write(myPath, lines, StandardCharsets.UTF_8,
StandardOpenOption.CREATE);
System.out.println("Data written to a file");
}
}
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
public class JavaWriteFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/sample.txt");
List<String> lines = new ArrayList<>();
lines.add("mango");
lines.add("apple");
lines.add("banana");
lines.add("watermelon");
lines.add("orange");
Files.write(myPath, lines, StandardCharsets.UTF_8,
StandardOpenOption.CREATE);
System.out.println("Data written to a file");
}
}Java Read File Example
We use Files.readAllLines() method to read all lines from a given file.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class JavaReadFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/sample.txt");
List<String> lines = Files.readAllLines(myPath, StandardCharsets.UTF_8);
lines.forEach(line -> System.out.println(line));
}
}Java Delete File Example
We use Files.deleteIfExists() method to delete a file if it exists.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaDeleteFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/sample.txt");
boolean fileDeleted = Files.deleteIfExists(myPath);
if (fileDeleted) {
System.out.println("File deleted");
} else {
System.out.println("File does not exist");
}
}
}Output:
File deletedReferences
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment