Thursday, February 19, 2009

JSP : Write Log Datewise

Writing logs in JSP..

Here is the code you can use to write log Every day Datewise(dd-mm-yyyy).
You can use in in you code JSP or Servlet.

you need to import following

import java.util.*;
import java.text.*;

Add following lines in your code accordingly

// open file to write date wise logs
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
java.util.Date date = new java.util.Date();
String today = df.format(date);

//Provide file in which log to be written
BufferedWriter outlog = outlog = new BufferedWriter(new FileWriter("D:\\Log_"+today+".log", true));

outlog.write("1st Line \n");........... //used "\n" as line seperator
outlog.write("2nd Line ");
outlog.newLine(); .....................//used outlog.newLine() as line seperator
outlog.write("3rd Line ");

//flush BufferedWriter at the end

Wednesday, February 11, 2009

Java : Sorting ArrayList in Ascending and Descending order

Simple Program to sort Arraylist in Ascending and Descending order. U can also sort String,Float,Double.. etc in ArrayList.

ArrayList can be sorted using inbuilt java 1.5 class method Collections.sort(..).

Here is code, save it as file :

import java.util.*;

public class Sorting
public static void main(String[] args)
//Initilize ArrayList
ArrayList<Integer> arrayList = new ArrayList<Integer>();//also it can sort String,Float,Double.. etc

//creating Random numbers
//Random is a inbuilt java class in 1.5 in util package
Random random = new Random();

//putting Values
for(int i=1;i<=10;i++)
int temp = random.nextInt(100);//get random int values between 0 and 100

//Sorting ArrayList
System.out.println("Before Sorting..");

Collections.sort(arrayList);//Sorting using sort method, default ascending sort

System.out.println("After Sorting Ascending..");

//useing Comparator to sort in descending order
Comparator<Integer> comparator = Collections.reverseOrder();
Collections.sort(arrayList,comparator);//Descending sorting using sort method

System.out.println("After Sorting Descending..");