package org.kodejava.util.zip;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipFileExample {
public static void main(String[] args) {
try {
// Create an instance of ZipFile to read a zip file
// called sample.zip
ZipFile zip = new ZipFile(new File("data.zip"));
// Here we start to iterate each entry inside
// sample.zip
for (Enumeration<?> e = zip.entries(); e.hasMoreElements(); ) {
// Get ZipEntry which is a file or a directory
ZipEntry entry = (ZipEntry) e.nextElement();
// Get some information about the entry such as
// file name, its size.
System.out.println("File name: " + entry.getName()
+ "; size: " + entry.getSize()
+ "; compressed size: "
+ entry.getCompressedSize());
System.out.println();
// Now we want to get the content of this entry.
// Get the InputStream, we read through the input
// stream until all the content is read.
InputStream is = zip.getInputStream(entry);
InputStreamReader isr = new InputStreamReader(is);
char[] buffer = new char[1024];
while (isr.read(buffer, 0, buffer.length) != -1) {
String s = new String(buffer);
// Here we just print out what is inside the
// buffer.
System.out.println(s.trim());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Category Archives: Java
What is Autoboxing?
Autoboxing is a new feature offered in the Tiger (1.5) release of Java SDK. In short auto boxing is a capability to convert or cast between object wrappers (Integer, Long, etc) and their primitive types.
Previously when placing primitive data into one of the Java Collection Framework object we have to wrap it to an object because the collection cannot work with primitive data. Also, when calling a method that requires an instance of object rather than an int or long, we have to convert it too.
Now, starting from version Java 1.5 we have a new feature in the Java Language which automate this process, its call the Autoboxing. When we place an int value into a collection, such as List, it will be converted into an Integer object behind the scene. When we read it back, it will automatically convert to the primitive type. In most way this simplifies the way we code, no need to do an explicit object casting.
Here an example how it will look like using the Autoboxing feature:
package org.kodejava.basic;
import java.util.HashMap;
import java.util.Map;
public class Autoboxing {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
// Here we put an int into the Map, and it accepted
// as it will be autoboxed or converted into the wrapper
// of this type, in this case the Integer object.
map.put("Age", 25);
// Here we can just get the value from the map, no need
// to cast it from Integer to int.
int age = map.get("Age");
// Here we simply do the math on the primitive type
// and got the result as an Integer.
Integer newAge = age + 10;
}
}
How do I use Java NIO to copy file?
The following code snippet show you how to copy a file using the NIO API. The NIO (New IO) API is in the java.nio.* package. It requires at least Java 1.4 because the API was first included in this version. The JAVA NIO is a block based IO processing, instead of a stream based IO which is the old version IO processing in Java.
package org.kodejava.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
public class CopyFileExample {
public static void main(String[] args) throws Exception {
String source = "medical-report.txt";
String destination = "medical-report-final.txt";
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(destination);
FileChannel inputChannel = fis.getChannel();
FileChannel outputChannel = fos.getChannel();
// Create a buffer with 1024 size for buffering data
// while copying from source file to destination file.
// To create the buffer here we used a static method
// ByteBuffer.allocate()
ByteBuffer buffer = ByteBuffer.allocate(1024);
// Here we start to read the source file and write it
// to the destination file. We repeat this process
// until the read method of input stream channel return
// nothing (-1).
while (true) {
// Read a block of data and put it in the buffer
int read = inputChannel.read(buffer);
// Did we reach the end of the channel? if yes
// jump out the while-loop
if (read == -1) {
break;
}
// flip the buffer
buffer.flip();
// write to the destination channel and clear the buffer.
outputChannel.write(buffer);
buffer.clear();
}
}
}
How do I split a string?
Prior to Java 1.4 we use java.util.StringTokenizer class to split a tokenized string, for example a comma separated string. Starting from Java 1.4 and later the java.lang.String class introduce a String.split(String regex) method that simplify this process.
Below is a code snippet how to do it.
package org.kodejava.lang;
import java.util.Arrays;
public class StringSplit {
public static void main(String[] args) {
String data = "1,Diego Maradona,Footballer,Argentina";
String[] items = data.split(",");
// Iterates the array to print it out.
for (String item : items) {
System.out.println("item = " + item);
}
// Or simply use Arrays.toString() when print it out.
System.out.println("item = " + Arrays.toString(items));
}
}
The result of the code snippet:
item = 1
item = Diego Maradona
item = Footballer
item = Argentina
item = [1, Diego Maradona, Footballer, Argentina]
How do I execute other applications from Java?
The following program allows you to run / execute other application from Java using the Runtime.exec() method.
package org.kodejava.lang;
import java.io.IOException;
public class RuntimeExec {
public static void main(String[] args) {
//String command = "/Applications/Safari.app/Contents/MacOS/Safari";
String command = "explorer.exe";
try {
Process process = Runtime.getRuntime().exec(new String[]{command});
} catch (IOException e) {
e.printStackTrace();
}
}
}
