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

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

Interview Question – Use Java Thread to Do Math Calculation

This is an example for showing how to use join(). Interview question: Use Java multi-threading to calculate the expression 1*2/(1+2). Solution: Use one thread to do addition, one thread to do multiplication, and a main thread to do the division. Since there is no need to communicate data between threads, so only need to consider … Read more

java io class hierarchy diagram

The large number of classes in the Java IO package is overwhelming and annoying. However, if we use Java, we still need to understand those classes. In fact, the classes in Java IO package is not very complex, but we need a good way to learn those. There are two important factors for understanding the … Read more

Parse HTML in Java

This code example shows how to parse HTML in Java by using jsoup. As there are many libraries for various purposes, there are a lot of html parser in Java. A lot of developers wonder which one is the best before they made a decision on an HTML parser. Jsoup is a very good start. … Read more

LeetCode – K Empty Slots (Java)

Given a garden with N slots. In each slot, there is a flower. Each of the N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will stay blooming since then. Given an array of numbers from 1 to N. Each number in … Read more

LeetCode – My Calendar II (Java)

Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking. Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start