CORE API / CATATAN</>↗Core API18 Feb 2010
The example below show how to use a FileChannel to read some data into a Buffer. We create a FileChannel from a FileInputStream instance. Because a channel reads data into buffer we need to create a ByteBuffer and set its capacity. Read data from a channel into buffer using the FileChannel.read() method. To read out […]
CORE API / CATATAN</>↗Core API16 Feb 2010
This post show you how to use the java.nio buffer such as ByteBuffer, CharBuffer, DoubleBuffer, etc. To create a buffer you can use the allocate(int capacity) method. Each buffer object has the allocate(int capacity) method. You need to pass the value of the capacity of the buffer to be created when calling this method. package […]
CORE API / CATATAN</>↗Core API13 Feb 2010
The example below show you how to copy a file using the java.nio.channels.FileChannel class. package org.kodejava.io; import java.io.*; import java.nio.channels.FileChannel; public class FileCopy { public static void main(String[] args) { //// Define the source and target file File source = new File("D:/Temp/source.txt"); File target = new File("D:/Temp/target.txt"); // Create the source and target channel try […]
SERVLET / CATATAN</>↗Servlet13 Feb 2010
The example below is a servlet that shows you how to create a zip file and send the generated zip file for user to download. The compressing process is done by the zipFiles method of this class. For a servlet to work you need to configure it in the web.xml file of your web application […]
GOOGLE GSON / CATATAN</>↗Google Gson10 Feb 2010
This example show you how to convert a java.util.Map into JSON string and back to Map again. package org.kodejava.gson; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; public class MapToJson { public static void main(String[] args) { Map<String, String> colours = new HashMap<>(); colours.put("BLACK", "#000000"); colours.put("RED", "#FF0000"); colours.put("GREEN", "#008000");...
GOOGLE GSON / CATATAN</>↗Google Gson08 Feb 2010
This example show you how to convert Java collections object into JSON string. For Student class use in this example you can find it the previous example on How do I convert object into JSON?. package org.kodejava.gson; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.kodejava.gson.support.Student; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class […...