How do you handle files in Java applications? This is very important to you know to programmatically you can Create, Write, Read, Delete (etc) file in Java. The Java File Handling comes under IO (input-output) package java.io. Under this package, classes provide all kinds of methods to handle the file in Java. But it’s not compulsory to use only java.io package, there is a more updated library and packages.
In this tutorial, you will learn about File Handling in java with a
Java File Handling
Here is some common and very useful File Handling operation in Java.
- Create File
- Write File
- Read File
- Move File
- Copy File
- Delete File
Let’s See the File Handling in Java with a simple program
One by one we will see the type of ways do different operation and one simple example of it.
#Create File
There are many ways to Create File in Java, some are-
- File.createNewFile(),
- FileOutputStream class
- and Java NIO.
Let’s see the example of using NIO writer method.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FileCreating { public static void main(String[] args) { String content = "EyeHunt data"; try { Files.write(Paths.get("newFile.txt"), content.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }
A link for depth knowledge about file creating and its examples follow this link – Java Create File
#Write File
A java have so many different ways to write a file in java. Here are some
- BufferedWritter
- FileWriter – Writes directly into the file
- PrintWriter – Write formatted text.s
- Write File using Java 7 Path
- more…(ways to write a file in java)
Here is example of Using Java 7 Path – write method to write data in a file.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileWriting { public static void main(String arg[]) { String fileContent = "Hello Java 7 Path"; try { Path path = Paths.get("samplefile.txt"); Files.write(path, fileContent.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }
Link for other examples- Java Write to File
#Read File
Ways to do read file in java:-
- FileReader class
- BufferedReader
- Scanner class
- Read text file using Files class
Let’s see the example with util scanner class to Read file in java. Scanner class comes under the java.util package.
import java.io.File; import java.util.Scanner; public class FileReading { public static void main(String[] args) throws Exception { // pass the path of file File file = new File("filename.txt"); Scanner sc = new Scanner(file); while (sc.hasNextLine()) System.out.println(sc.nextLine()); } }
More Example link – Java Read File Text | Into String
#Move File
How you can Move File in Java:-
- Files.Path move() method
- Using 2 methods – Java.io.File.renameTo() and Java.io.File.delete() methods:
Using a File move() method example.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class Hello { public static void main(String arg[]){ try { Files.move(Paths.get("test.txt"), Paths.get("dest/test.txt")); System.out.println("Successfully moved file"); } catch (IOException e) { e.printStackTrace(); } } }
Detailed tutorial on Moving file in java follow this link – Java Move File | Directory
#Copy File
Java have many ways to Copy a file in java, Here is some methods to do it:-
- NIO classes in java – java.nio.channels.FileChannel
- Apache Commons IO FileUtils
- Files class – copy() method
Let’s see the example with – Files class – copy() method.
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class CopyFileExample { public static void main(String[] args) { File oldFile =new File("test.txt"); File newFile =new File("cook.txt"); try { copyFileUsingJava7Files(oldFile,newFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); } }
To learn copy file in java follow this link – Java Copy files
#Delete File
Here is example of how to delete file in java.
import java.io.File; public class DeleteFile { public static void main(String[] args) { //absolute file name with path File file = new File("newfile.txt"); if(file.delete()){ System.out.println("File deleted"); }else System.out.println("File doesn't exists"); } }
A link of detailed tutorial on delete file in java – Java Delete File | Remove | If Exists
In Java file handling you can update a file using write methods.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All File Handling in Java Example Programs is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.