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

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

Java code for connecting MS SQL Server by using SQL Server Authentication

First of all, You will need to add a jar file to your project library as SQL Server 2000 Driver for JDBC Service. My target is SQL Server 2000, it will require the jar file called “sqljdbc4.jar”. This is not supported on Microsoft website now, you can download it here. For other versions of SQL Server, here is the link of SQL Server 2000 Driver for JDBC Service.

The following is the code for connection MS SQL Server and select some records from a testing table.

Read more

Java connect MS SQL Server using windows authentication

To connect MS SQL Server using windows authentication, the first step is to setup ODBC. You can go to Control panel -> Administrative tools -> ODBC. Add a new DSN to connect MS SQL Server using windows authentication account following wizard setup. The second step is the similar with using SQL Server authentication. The only … 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