CORE API / NOTES</>↗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 / NOTES</>↗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 / NOTES</>↗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 / NOTES</>↗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 […...
GOOGLE GSON / NOTES</>↗Google Gson04 Feb 2010
In the example below you can see how to convert an array into JSON string. We serialize the array to JSON using the Gson.toJson() method. To deserialize a string of JSON into array we use the Gson.fromJson() method. package org.kodejava.gson; import com.google.gson.Gson; public class ArrayToJson { public static void main(String[] args) { int[] numbers = […]
GOOGLE GSON / NOTES</>↗Google Gson03 Feb 2010
In this example you’ll see how the Gson library handles the object fields. For object fields to be serialized into JSON string it doesn’t need to use any annotations, it can even read private fields. If you have a null value field it will not be serialized into JSON string. To exclude a field from […]