Java Method to Shuffle an Array

How to shuffle an array in Java? There are two approaches to shuffle an int array(randomizes the order of the elements in an array), one is to use the Collections.shuffle() method, the other is to manipulate array elements. Approach 1: Shuffle elements in an array This is flexible, and easy to be changed to fit … Read more

Java Code – Convert a file to a String

How to read file content into a string? The following is the Java code to do that. To make it work, the filePath need to be changed. public static String readFileToString() throws IOException { File dirs = new File("."); String filePath = dirs.getCanonicalPath() + File.separator+"src"+File.separator+"TestRead.java";   StringBuilder fileData = new StringBuilder(1000);//Constructs a string buffer with … Read more

Java method for spliting a camelcase string

This Java method accepts a camel case string, and returns a linked list of splitted strings. It has been designed to handle various kinds of different cases and fully tested and good to use. Handling camel case in Java involves a lot of styles, this methods can handle the following cases: Regular camel case names, … Read more

Java: get a set of random numbers from a specified range

This is a method that accepts a range (max) and a number (n), returns an array of n random numbers. n: size of random number array max: largest available number (exclusive) For example: Input: n=5, max=100 Output: an array containing 5 random numbers whose range is from 0 to 99. public static HashSet<Integer> getNRandom(int n, … Read more

Sort content in a txt file

If you have a file with come words or terms on each line, you may want to sort it. Java Arrays.sort is common nice function to do this. Collections.sort() is another nice say. Here is an example and code.
E.G. in a file, you have the following txt.

dog 
cat
--windows
--kankan
pps
game
--annot be guaranteed 
as it is, generally speaking, 
--impossible to make any hard gu
arantees in the p
--resence of unsynchr

Read more

Eclipse: java.lang.UnsupportedClassVersionError: Bad version number in .class file

While programming for a small project under Eclipse IDE, I got the following error message: java.lang.UnsupportedClassVersionError: Bad version number in .class file The problem is the compilation JRE and running JRE are not the same. So we need to make them the same version, e.g. 1.6. There also might be the JRE library in your … Read more

How to use java properties file?

For configuration purposes, using properties file is a good way of reusing. In this way, when the code is packaged to a jar file, other users can just put the different configurations in the config.properties file. The following is a simple example of using properties file. 1. create the file hierarchy like the following. Mainly … Read more

Convert java jar file to exe

By using eclipse export wizard, you can get executable jar files. But the jar file can be only launched by using command lines. Apparently, this is not a good option for most regular not-java-programmer users, especially if the program requires user’s input to proceed. We need a program which can be started by double clicking. … Read more

Java code: Open IE browser and close it

I want to refresh my blog on Sohu to increase the visitor count. The following is the code for open IE browser and then close it. This will be done every 5 seconds, and in one day the count should be able to increase 3600*24/5 = 17280. So if you open your computer 10 days,  the count will become 172800. That will be a nice number.

Read more

Using Java to read web page

The following is the code for reading html content of a web page. Using java to read web page is different from using Java to call IE to open web pages. The visitor count for a general blog can not increase in this way. To increase visitors count, you need a program that can call … Read more

Java: convert a file to a byte array, then convert byte array to a file.

In this post, I will show you how to convert a file to a byte array and then convert a byte array to a file. To convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically … Read more

Java: convert image to byte array, convert byte array to image

This post shows two different ways to convert an image to a byte array and convert a byte array to an image. First of all, the byte type in Java is an 8-bit signed two’s complement integer. Its range is [-128, 127]. A byte array is just an array of bytes. An image is essentially … Read more