Wednesday, August 27, 2008

Java: Distinct List Function

The below java function will distinct a list of string that splitted by coma.

For example:
If input = A,A,B,B,C
This function will return A,B,C in Vector type.



Vector DistinctList(String lists){
   int a=0;
   int i=0,j=0,k=0;
   boolean dup = false;
   Vector vPreConCols = new Vector();
   String[] L1 = split(lists,",");    vPreConCols.add(L1[0]);
   a=1;
    for(i=1; i<L1.length; i++){
       j=0;
          while((j<a)&&(dup==false)){
            if(L1[i].equalsIgnoreCase(vPreConCols.get(j).toString())){
                dup=true;
            }
            j+=1;
         }
          if(!dup){
            vPreConCols.add(L1[i]);
         a+=1;
          }
   }
return vPreConCols;
}

Thursday, August 21, 2008

Jsp: Set Browser Expired Time

Command:
session.setMaxInactiveInterval(int intSecond);
intSecond -> time in second

Example:
session.setMaxInactiveInterval(10);

Description:
The browser will expire within 10 seconds

Sunday, August 17, 2008

Java: Increase Heap Size to prevent java.lang.OutOfMemoryError

java -Xms -Xmx Eg: java -Xms32m -Xmx128m javaFileName
* By default the maximum heap size = 128 mb

Sunday, August 10, 2008

Java: Redirect Java Output to a File

In order to redirect the java standard output to a file, we need to use the greater-than-sign (>) character followed by a filename after java.exe on a command line.
For example:
java Hello > D:\output.txt
This will redirect the output of System.out.println() to output.txt file

Saturday, August 9, 2008

Java: Get Current Directory

import java.io.File;
   public class CurrentDir {
      public static void main (String args[]) {
         File dir1 = new File (".");
         File dir2 = new File ("..");
         try {
            System.out.println ("Current dir : " + dir1.getCanonicalPath());
            System.out.println ("Parent dir : " + dir2.getCanonicalPath());
         }
         catch(Exception e) {
            e.printStackTrace();
         }
      }
}

Java: Rename a file

import java.io.*;

public class ListFile {
   public static void main(String args[]){
         ListFile lstFile = new ListFile();
         System.out.println(lstFile.renameFile("D:/test/nasatest.txt"));
   }

   public boolean renameFile(String file){
         File oldFile = new File(file);
         File newFile = new File("D:/test/nasa.txt");
         boolean success = false;
         // Rename file (or directory)
         try{
               success = oldFile.renameTo(newFile);
         }catch(Exception e){
               e.printStackTrace();
         }
         return success;
   }
}

Saturday, August 2, 2008

Java: Download files from HTTP

import java.io.*;
import java.net.*;
/*
* Command line program to download data from URLs and save
* it to local files. Run like this:
* java FileDownload http://schmidt.devlib.org/java/file-download.html
* @author Marco Schmidt
*/
public class FileDownload {
   public static void download(String address, String localFileName) {
      OutputStream out = null;
      URLConnection conn = null;
      InputStream in = null;
      try {
         URL url = new URL(address);
         out = new BufferedOutputStream(
         new FileOutputStream(localFileName));
         conn = url.openConnection();
         in = conn.getInputStream();
         byte[] buffer = new byte[1024];
         int numRead;
         long numWritten = 0;
         while ((numRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, numRead);
            numWritten += numRead;
         }
         System.out.println(localFileName + "\t" + numWritten);
      } catch (Exception exception) {
         exception.printStackTrace();
      } finally {
         try {
            if (in != null) {
               in.close();
            }
            if (out != null) {
               out.close();
            }
         } catch (IOException ioe) {
         }
      }
   }

   public static void download(String address) {
      int lastSlashIndex = address.lastIndexOf('/');
      if (lastSlashIndex >= 0 &&
      lastSlashIndex < address.length() - 1) {
         download(address, address.substring(lastSlashIndex + 1));
      } else {
         System.err.println("Could not figure out local file name for " +
address);
         }
      }

   public static void main(String[] args) {
      for (int i = 0; i < args.length; i++) {
         download(args[i]);
      }
   }
}

Reference: http://schmidt.devlib.org

Java: Delete files in a directory

public void deleteFiles(){
   java.io.File file = new java.io.File("d:\\test");
   java.io.File[] f = file.listFiles();
   for (int i = 0; i < f.length; i++) {
      f[i].delete();
   }
}

Since 26 July 2008: