[Java] Download File from URL

In your program, you might want to acquire files from the internet for purposes such as update, additional resources, web image gallery… so here’s a modified version of the codes I found on stackoverflow.com that you can just copy and paste into your Java program. Just supply the URL for path and the file name in filename.

 public static String downloadFile(String path, String filename)

 try {
 URL website = new URL(path);
 ReadableByteChannel rbc = Channels.newChannel(website.openStream());
 FileOutputStream fos = new FileOutputStream(filename);
 fos.getChannel().transferFrom(rbc, 0, 1 << 24);
 } catch (IOException ex) {
 Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);

 return path + "............ COMPLETED!";


[Java] Importing / Exporting Data to Excel

In the engineering world, we often gather lots of data that we need to store and display in a spreadsheet format. Microsoft’s Excel is probably the most popular spreadsheet format there are out there. In order to read and write to an excel file from Java, we need the Apache POI library. The POI library actually handle all kinds of Microsoft document format, however, we will only deal with Excel files today.

Apache POI Library Homepage

Apache POI’s Detailed Programming Guide

Continue reading

[Engineering] Arduino to Java Serial Communication 2

In this post, I will talk about using Java as a client and Arduino as a server. To keep it simple, we will only use the Arduino’s built-in LED 13. Before proceeding, please read my other post about Arduino to Java Communication through Serial Port if you haven’t already.

In summary, the only differences in the Java’s side is that we will use jSSC’s serial write method instead of serial read as follows:


Continue reading

[Engineering] Arduino to Java Serial Communication

Arduino is awesome, but it is pretty much useless as a data acquisition (DAQ) device if we can only read the data in the Arduino IDE. I remember back in Freshman year, worked on a wireless board for my Intro to Engineering project. Back then, not knowing anything really from programming, I decided that it would be cool to make a wireless remote game controller like the Wii controller. So I dug right into it, using the manufacturer’s sample program to get the data I need, write it into a text file, import it into a game I wrote with Game Maker… omg, the errors, the lags. The TA tried to help but it was hopeless, lol. Then I did it again with the Space Hopper project, if I only know how to read Joystick data in Java…

Anyway, did a bit of coding today and this is what I got after messing around for couple hours. Most time was spent trying to get Arduino to send the data out correctly. C++ is just… not my thing. Will probably write a Java Class for it to make it even easier to use.


Continue reading

Launching Java Programs with Batch File (Console Display)

Writing a batch file to launch Java program is relatively easy. It is useful if you need to console output something.

  • Create a new Text File
  • Type in following code :
  • java -jar "YOUR JAVA PROGRAM NAME.jar"
  • Change the file extension from “.txt” to “.bat”

Here’s how to change file extension:

[Java] GUI Console Output

Normally, if you write an application, to get the console output, you need to either load it in command propmt or write a batch file to have the Jar file launched with the command prompt window. This method might not work at some locations (ie. Comapny Shared Folder).

To solve this problem, we can create out own Output window in Java, and redirect  System.out to that window by overriding the methods.

Continue reading

[Java] ArrayList – sorting

Let’s say I am writing a program to keep track of amount of hours we spent on testing each engine, I need to utimately generate a report. It will be nice if I can sort the Engine based on it’s Serial number. So here’s the easy way to sort an ArrayList.

We sill use the method:

Collections.sort(list, comparator);

Continue reading