Loop Through a Given Directory With Indentation in Java

The code below provide a method for recursively loop through a directory in Java. It also prints out the directory hierarchy with indentation. package com.programcreek;   import java.io.File; import org.apache.commons.lang3.StringUtils;   public class LoopThroughADirectory {   public static void main(String[] args) { File[] files = new File("/home/programcreek/Desktop").listFiles(); showFiles(files); }   public static void showFiles(File[] files) … Read more

Move File in Java

If you want to move a file from one directory to anther in Java, you can use rename() method of File. The following is a simple code example for moving a file called “s1” to another directory. package com.programcreek;   import java.io.File;   public class MoveFileExample {   public static void main(String[] args) { File … Read more

Java Merge Two Directories to One

This is a method I write for merging two directories. The directory of second parameter will be moved to the first one, including files and directories. package com.programcreek;   import java.io.File;   public class MergeTwoDirectories { public static void main(String[] args){ String sourceDir1Path = "/home/programcreek/Desktop/d1"; String sourceDir2Path = "/home/programcreek/Desktop/d2";   File dir1 = new File(sourceDir1Path); … Read more

Read bytes from FileInputStream in Java

Read bytes from FileInputStream The following example shows how to read bytes from FileInputStream in Java. import java.io.File; import java.io.FileInputStream; public class fileInputStream { public static void main(String[] args) { byte[] data = new byte[1024]; //allocates memory for 1024 bytes //be careful about how to declare an array in Java int readBytes; try { File … Read more

Delete File in Java

Deleting files in Java is really simple. You can just use delete method of File. Here is a sample code: package file;   import java.io.File;   public class DeleteFile { public static void main(String[] args) { File file = new File("/home/programcreek/Desktop/s1");   if (file.delete()) { System.out.println(file.getName() + " is deleted!"); } else { System.out.println("File is … Read more

Merge Files in Java

I often need to merge multiple files into one in Java. So I write a reusable method to do this job. It works very well for me to merge a set of txt files. The method accepts an array of File and the merged file path. After running the method, the set of files to … Read more

Java 7 File Copy Example Code

Before Java 7, if we need to copy a file we need to make a method or call the copyFile(File srcFile, File destFile) method of FileUtils inapache commons io package. In Java 7, file copy function is very simple. The following is a code example for showing how to use Files.copy() method: import java.io.IOException; import … Read more

Java append/add content to an existing file

Replace vs Append/Add If you want your code to create a new file and erase previous existing file, FileWriter can simply take it place. To replace all content in an existing file, use this: FileWriter fstream = new FileWriter(loc);FileWriter fstream = new FileWriter(loc); The code above will delete the existing file if it’s name is … Read more

How to Write a File Line by Line in Java?

This post summarizes the classes that can be used to write a file. 1. FileOutputStream public static void writeFile1() throws IOException { File fout = new File("out.txt"); FileOutputStream fos = new FileOutputStream(fout);   BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));   for (int i = 0; i < 10; i++) { bw.write("something"); bw.newLine(); }   bw.close(); … Read more

Java read a file line by line – How Many Ways?

Processing a text file line by line is a common thing programmers do. There are many related classes in the Java I/O package and this may get confusing. This post shows 4 different ways of reading a file line by line in Java. 1. FileInputStream and BufferedReader private static void readFile1(File fin) throws IOException { … Read more