Today we will look at java character stream writer. Standard implementation FileWriter class is used to write character stream into the underlying file system. FileWriter class provides API to write character by character, character array and String. BufferedWriter is a process stream used to decorate the IO stream FileWriter. The process stream BufferedWriter helps to [...]
Continue reading…
Core Java
Core Java Tutorials, Core Java Programming Tips, Simple Core Java Tutorial
Format date using thoughtworks Date Converter.
Today we will look at one very useful utility class named DateConverter which is part of the thoughtworks xstream library . This class can parse date from almost any given format. In the below given example the DateConverter class takes two constructor arguments, first one is default date format and second one is an array [...]
Continue reading…
How to read a text file line by line in java?
One of the java.io.reader implementations named BufferedReader gives efficient API to read character input line by line. As you can assume from the name of the class itself that it create an in memory cache or buffer of the given file and load/re-load the buffer on demand. If you are working with a large file [...]
Continue reading…
How to reload property file in java at runtime?
I have found a good utility class which is part of the apache configuration jar to reload the property or resource file without restarting the JVM or Application Server. FileChangedReloadingStrategy class can be used to reload the property/resource file at runtime if there is any modification is done in the property file. In the below [...]
Continue reading…
How to find the heap memory usage of your JVM instance?
Java JMX API has very simple API to find out the memory usage of a JVM instance. In the below given example we will find the memory usage of a JVM instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.ourownjava.corejava.util; import java.lang.management.ManagementFactory; import java.lang.management.MemoryUsage; /** * * @author ourownjava.com * @date 28th June, 2011 * * Example program to find the memory usage of JVM * */ public class HeapMemoryUsageExample { public static void main(String args[]){ final MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean(). getHeapMemoryUsage(); System.out.println("Allocated memory for JVM :"+heapMemoryUsage.getCommitted()); System.out.println("Initially requested memory :"+heapMemoryUsage.getInit()); System.out.println("Maximum memory can be used :"+heapMemoryUsage.getMax()); System.out.println("Memory used by the JVM :"+heapMemoryUsage.getUsed()); } } |
Console output
1 2 3 4 5 6 |
/mnt/opt/java com.ourownjava.corejava.util.HeapMemoryUsageExample Allocated memory for JVM :5177344 Initially requested memory :0 Maximum memory can be used :66650112 Memory used by the JVM :205096 |
java 7 supports multiple exception handling in same catch block.
One of the most useful feature added as part of java 7 Coin project is capability to handle more than on exception in same catch block. Less non business code makes code more readable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.ourownjava.corejava.exception; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * * @author ourownjava.com * * Handling multiple exception in one catch block. * */ public class MultiExceptionHandingInOneCatchBlock { public static void main(String args[]){ new MultiExceptionHandingInOneCatchBlock().catchMany(); } @SuppressWarnings({"unused", "resource"}) public void catchMany() { try{ final FileInputStream inputStream = new FileInputStream(new File("test.txt")); final Connection connection = DriverManager.getConnection("none"); }catch(SQLException | IOException e){ e.printStackTrace(); } } } |
Wildcard File Filter in java.
Apache common give a useful class named WildcardFileFilter to filter/open files using a wildcardfilter. A simple example of wild card filter is *_2012-01-10*.txt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.ourownjava.corejava.file; import java.io.File; import java.io.FileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; /** * * @author ourownjava.com * */ public class FileWildcardFilter { private static File[] loadFiles(String location, String fileName) { final File dir = new File(location); final FileFilter fileFilter = new WildcardFileFilter(fileName); return dir.listFiles(fileFilter); } public static void main(String args[]) throws Exception { final File[] files = loadFiles("\tmp", "08-12-11_*feed*.dat"); for (File file : files) { System.out.println(file.getName()); } } } |
How to identify deadlock in java?
Java Management Extension provides a powerful APIs to find out all details about the JVM. Today we will write a simple program to find out dead lock in java using JMX API. The intention of this program is only to find the dead lock, the program which is used to create the dead lock should [...]
Continue reading…
How to find all live threads in JVM?
Java JMX API provides you some useful classes to find out details about your JVM. One of those classes is ManagementFactory, This class provides API to find meta data about the JVM. In the below given example we will find all the live threads in the JVM and its state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.ourownjava.corejava.thread; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; /** * @author ourownjava.com * @date 28th June, 2011 * * Example program to find all live threads in your JVM instance. * */ public class ManagementFactoryExample { public static void main(final String args[]){ final WorkerThread worker = new WorkerThread(); worker.setName("workerThread"); worker.start(); final ThreadInfo[] threadInfos = ManagementFactory.getThreadMXBean(). getThreadInfo(ManagementFactory.getThreadMXBean().getAllThreadIds()); for(final ThreadInfo threadInfo : threadInfos){ System.out.println(threadInfo.getThreadId()); System.out.println(threadInfo.getThreadName()); System.out.println(threadInfo.getThreadState()); } System.out.println("Total number of live threads :"+ManagementFactory.getThreadMXBean().getThreadCount()); } } class WorkerThread extends Thread{ public void run(){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } |
Console output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/mnt/opt/java com.ourownjava.corejava.thread.ManagementFactoryExample 8 workerThread TIMED_WAITING 5 Attach Listener RUNNABLE 4 Signal Dispatcher RUNNABLE 3 Finalizer WAITING 2 Reference Handler WAITING 1 main RUNNABLE Total number of live threads :6 |
How to convert native character to unicode and unicode to native?
Java provides a simple tool named native2ascii to convert native to ASCII and ASCII to native. Native characters are represented in ASCII using its Unicode equivalent. You can find the native2ascii tool in the JAVA_HOME/bin To convert native to ascii you can use the below given command. /local/opt/java/bin/native2ascii -encoding utf8 source-file target-file source file should [...]
Continue reading…