Skip to main content

Posts

Showing posts from October, 2012

"Fastest" way copy a file in java.

Java didn’t implement any ready make file copy function. To copy file, read the file into a bytes stream with FileInputStream and write the bytes into another file with FileOutputStream . static final int BUFF_SIZE = 100000 ; static final byte[] buffer = new byte[BUFF_SIZE] ; /** * @param from * @param to * @throws IOException */ public static void copyCustomize(String from, String to) throws IOException { InputStream in = null ; OutputStream out = null ; try { in = new FileInputStream ( from ) ; out = new FileOutputStream ( to ) ; while ( true ) { synchronized ( buffer ) { int amountRead = in . read ( buffer ) ; if ( amountRead = = - 1 ) { break ; } out . write ( buffer , 0 , amountRead ) ; ...

Singleton Design Pattern in Java

Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM. The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes). A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object. One such scenario where it might prove useful is when we develop the help Module in a project. Java Help is an extensible, platform-independent help system that enables authors and developers to incorporate online help into applications. Singletons can be used to create a Connection Pool . If programmers create a new connection object in ever...

Simple Factory design pattern in java

Factory pattern is a kind of Creational Pattern. Download Source code How the Factory work: Given : Somewhere people write First name first and then last name but some where peole use backward way. If we fill a full name with comma, then the word is place before comma will be last name, and if we enter a full name without comma then the last word is last name.                          In the below figure, there's a parent class Namer, and two child classes: FirstFirst and LastFirst. The NameFactory decides which sub classe to return depending on the input.      

Auto remove unused imports in eclipse!

    Sometime we get a warning with unused imports! It's easy to remove them automaticaly!     1. Window-->References         -->Selecte node: Java-->Editor-->Save Actions.     2. Click on button Configure...         Select Unnecessary Code tab --> check Remove unused imports Good luck!

[Plug-in]Java decompiler plugin for Eclipse

Download the package here: http://www.kpdus.com/jad.html Or download here:  JAD plugin   1. Copy net.sf.jadclipse_3.3.0.jar to Eclipse plugin folder \eclipse\plugins\ 2. Restart Eclipse to make the plugin take effect. 3. Configure Jadclipse in Eclipse In Eclipse, Click Window –> Preference –> Java –> Jadclipse , Key in Jad’s path in "Path to Decompiler" field. (ex: c:\tool\jad.exe) 4. Enjoy :)