Archives: Java Stuff

Java Zip API: How to Create a Zip File Using Java

July 11, 2010 by Rakhitha Karunarathne | 5 Comments | Filed in Java Stuff, Tech

This is a follow-up to my earlier post “Java Zip API: How to Read a Zip File Using Java“. This time it’s about how to create a zip file using Java. This is even simpler than reading a zip file.

Following is a very simple example which create a zip file named RaKasUniverse.zip. Inside that zip file we will create a directory named MyDirectory and inside that, we will have a file named MyFile.txt. That text file will contain the text “Java Zip API: How to Create a Zip File Using Java”. In the example we will be using following classes from Java API.
(more…)

Tags: , , , , ,

Fun with Java Reflections: Access Private Member Fields/Methods of other Classes

June 12, 2010 by Rakhitha Karunarathne | No Comments | Filed in Java Stuff

This is a follow-up to my earlier post on Java Reflections.  In the earlier post I just showed how to invoke methods of an object dynamically using reflection. Here I am going to talk about accessing attributes of another object using reflection, with a little twist.

Just like my earlier post I’ll start this with an exercise.

Write a class with a private member variable only. No get/set methods. But add a toString() method to output the content of that private variable.

Like Following….

Now write another class. It should create an instance of above class, and set your name as the value of ‘name’ attribute in that object.

(more…)

Tags: , , , , , , , , , , , ,

Java Core API: Introduction to Java Reflections

June 12, 2010 by Rakhitha Karunarathne | 6 Comments | Filed in Java Stuff

Here is an exercise for you!

Write a java program that take two or more command line arguments. 1st argument is a class name. 2nd argument is a method name. Rest of the arguments are string values to be passed in to that method.

Your program has to load the class specified in first argument (this class may not be available at compile time). Create an instance of that class. Then invoke the method given in the 2nd argument on that object passing the values given in rest of the arguments as parameters to that function. To simplify things lets say all parameters are string.

The answer is reflections. To give you a very simplified description, Reflections allow you to access members of a class/object dynamically in run time without having to hard-code things in the source code. This is slower than hard-coding. But it is very useful when the class/methods/fields that you need to access are not known or not available at compile time. Also in some cases you can do cool things like accessing private members.

(more…)

Tags: , , , , , , , ,

Java Zip API: How to Read a Zip File Using Java

June 11, 2010 by Rakhitha Karunarathne | 3 Comments | Filed in Java Stuff

Java API give a very simple set of classes to deal with zip files in its java.util.zip package. I am just going to show how to access the content of a zip file using these classes. Basically in java point of view a zip file is a collection of zip entries. you just select the zip entry that you like to read and extract its content using an InputStream. However this API do not support handling of password protected zip files.

(more…)

Tags: , , , ,

Me, My Self and Java

June 11, 2010 by Rakhitha Karunarathne | No Comments | Filed in Java Stuff, Me, Myself & I, Tech

Programming is my main hobby. I wrote my first computer program when I was 11 years old. Then I was using GW BBASIC. Since then I have learned QBASIC, Pascal, Visual Basic, Perl, Java, PLSQL, C++,  PHP, C#, bit of Python and many other things over time. I screwed up  my A/L for it but fortunately managed to get in to a career in Software Development. When I was a school kid I built some web sites using ASP and Perl but they were never published. And I spent a great deal of time learning regular expressions. Graphics Design was not my thing. So I ended up as an applications programmer.

(more…)

Tags: , , , , , , , , , , , , , , , , , , ,