APACHE COMMONS / NOTES</>↗Apache Commons05 May 2006
In this example we use FileUtils class from Apache Commons IO to read the content of a file. FileUtils have two static methods called readFileToString(File) and readFileToString(File, String) that we can use. An example to read file contents and return the result as a string can be seen below. package org.kodejava.commons.io; import org.apache.commons.io.FileUtils; import java.io.File; […]
ZIP AND GZIP / NOTES</>↗Zip and GZIP05 May 2006
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 […]
BASIC / NOTES</>↗Basic04 May 2006
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 […]
CORE API / NOTES</>↗Core API03 May 2006
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 […]
CORE API / NOTES</>↗Core API01 May 2006
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 […]
CORE API / NOTES</>↗Core API30 Apr 2006
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(); } } }