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

10 minutes Perl tutorial for Java developer

This 10 minutes tutorial is not meant to compare Java with Perl. The purpose is to explore how to quickly learn Perl as a Java developer. The following are some key notes from my perspective. 1. Basics to Start Unlike Java, there is no need to claim a “main” method as entry point. To run … Read more

Sort a String LinkedList in Java

Sorting a string LinkedList in Java is easy. You can sort the string LinkedList in ascending alphabetical order by using sort(List<T> list) . You can also sort the string LinkedList in descending alphabetical order by using sort(List<T> list, Comparator<? super T> c) . Here is the code: public static void main(String[] args) { LinkedList<String> stringList … Read more

Learn Eclipse RCP framework by using open source projects

Open source is not a new concept and you can find open source projects for almost all commonly used frameworks. In 2010, UCI published their project corpus which contains about 18,000 open source java projects. We can easily search and find projects that use popular frameworks such as Eclipse RCP, Struts 2, Spring, Android, etc. … Read more

Magento is an open-source eCommerce platform developed by Varien Inc that is widely used for online businesses. varien.io is a web hosting company specialized in WordPress based sites. AceWordPress is a free WordPress-based content management system, developed by the author of WordPress and built by a team of developers in cooperation with the community. wotube.com … Read more