Mathzprasad's Blog

August 24, 2009

New file each time

Filed under: java — mathzprasad @ 12:21 PM
Tags: , ,

When using Log4j, if we want to create a new file each time, instead of appending to the existing log, we can configure it using “append=false”.

Following is an example log4j.properties file.

log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
log4j.appender.A1.File=Log.log
log4j.appender.A1.append=false

August 20, 2009

SystemClassLoader

Filed under: java — mathzprasad @ 3:14 PM
Tags: , ,

Had a requirement to include all the icons in my jar file.
But my java code was not picking it up.
It did not recognize the image files in the icons folder.

In the eclipse environment, the following worked fine. Icons was a separate folder outside the source package.
Image image = new Image(display,”icons\\logo.gif”);
When included in the jar the above code did not work.

For getting the icons as resource, we resorted to the following method.
Image image = new Image(display,this.getClass().getClassLoader().getResourceAsStream(“icons/logo.gif”));
But this loads the resources from the class directory and the sub directories.
This did not pick up the icons which are packed in the jar outside the src folder.

Finally, we used the system classLoader, which served the purpose.
Image image = new Image(display,ClassLoader.getSystemResourceAsStream(“icons/logo.gif”));

For more information, refer:

http://www.coderanch.com/t/369908/Java-General/java/ClassLoader-getSystemResourceAsStream

and the java doc

http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemClassLoader()

August 18, 2009

Standard Widgets Toolkit (SWT)

Filed under: java — mathzprasad @ 5:14 PM
Tags: , ,

While working for a project, I came to know about Standard widgets toolkit.
The requirement was to develop a java stand alone application, that can understand drag/drop from Lotus Notes. After analysing Swing and SWT a lot, SWT was chosen as the right candidate.
SWT is the software component that delivers native widget functionality in an operating system independent manner. It is analogous to AWT/Swing in Java with a difference – SWT uses a rich set of native widgets.

SWT is an open source technology built with the Eclipse framework.

http://www.eclipse.org/articles/Article-SWT-Design-1/SWT-Design-1.html

Every SWT application requires a Display and one or more Shells. The Shell is a composite object; it can contain other composite objects. If the layout of the shell is not set, widgets added to the Shell won’t be visible. The Shell window must be opened to be displayed. The event handling loop reads and dispatches GUI events. If there is no event handling loop, the application window cannot be shown, even if the Shell window is opened by the open() method. Afterwards, you should dispose of the Display, when the Shell is discarded.

Building SWT applications can be made easy using the SWT designer. Placing the UI components like labels, buttons, text boxes etc., will be a hectic task without a designer tool.
SWT Designer provided by instantiations served the purpose.

They also provide another tool – WindowTester, a JUnit compatible eclipse pluggin for unit testing the SWT applications.

August 10, 2009

Zip files in java

Filed under: java — mathzprasad @ 1:12 PM
Tags: ,

package zip;
import java.io.*;
import java.util.zip.*;

public class ZipCreateExample{
 public static void main(String[] args) throws IOException{
  String filesToZip = “C:\\samples\\jasonchat.txt”;
  File f = new File(filesToZip);
  if(!f.exists()){
   System.out.println(“File not found.”);
   System.exit(0);
  }

  String zipFileName = “C:\\samples\\jasonchat.zip”;
  if (!zipFileName.endsWith(“.zip”))
   zipFileName = zipFileName + “.zip”;
  byte[] buffer = new byte[18024];
  try{
   ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
   out.setLevel(Deflater.DEFAULT_COMPRESSION);
   FileInputStream in = new FileInputStream(filesToZip);
   out.putNextEntry(new ZipEntry(filesToZip));
   int len;
   while ((len = in.read(buffer)) > 0){
    out.write(buffer, 0, len);
   }
   out.closeEntry();
   in.close();
   out.close();
  }
  catch (IllegalArgumentException iae){
   iae.printStackTrace();
   System.exit(0);
  }
  catch (FileNotFoundException fnfe){
   fnfe.printStackTrace();
   System.exit(0);
  }
  catch (IOException ioe){
   ioe.printStackTrace();
   System.exit(0);
  }
 }
}

Delete directory

Filed under: java — mathzprasad @ 12:42 PM
Tags: ,

Following code deletes all the files in the given directory/folder.

 package files;

import java.io.File;

public class DirDelete {
 public static void main(String[] args) {
  deleteDir(new File (“C:\\sample\\dir1″));
 }
 public static boolean deleteDir(File dir) {

  boolean success = true;
  if (dir.isDirectory())
  {
   String[] children = dir.list();
   for (int i=0; i<children.length; i++) {
    try {
     File f = new File(dir, children[i]);
     if (f.isDirectory()){
      deleteDir(f);
     }
     f.delete();
     dir.delete();
    } catch (Exception e) {
     success = false;
     e.printStackTrace();
    }
   }
  }
  return success;       
 }
}

DateFormatter in Java

Filed under: java — mathzprasad @ 6:12 AM
Tags: , , ,

This is a simple date formatter in java.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParseDate {
public static void main(String[] args) {
 String a = “Thu Jul 16 06:15:31 IST 2009″;
 String dateAsStr = “”;
 DateFormat df = new SimpleDateFormat(“EEE MMM d HH:mm:ss z yyyy”);
 DateFormat outFormat = new SimpleDateFormat(“MM/dd/yyyy HH:mm:ss a”);
 df.setLenient(true);
 
 Date dt = null;
 try
 {
  dt = df.parse(a);          
  dateAsStr = outFormat.format(dt);
  
 } catch (ParseException e)
 {
  e.printStackTrace();
 }
 System.out.println(“dt “+ dateAsStr);
}
}

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.