Monday, January 28, 2013

copy,delete and rename a file

File oldfile =new File("C:/Users/Boomiraj/Desktop/palani.txt");
        File newfile =new File(oldfile.getAbsoluteFile()+".tmp");

        /*if(oldfile.renameTo(newfile)){
            System.out.println("Rename succesful");
        }else{
            System.out.println("Rename failed");
        }*/
        try {
            FileUtils.copyFile(oldfile, newfile);
           
            if(oldfile.delete()){
                System.out.println("File deleted");
            }
            if(newfile.renameTo(oldfile)){
                System.out.println("File rename");
            }
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Friday, January 18, 2013

Pagination test code

check.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./CSVLoginServlet" method="post">
<input type="submit" value="Submit">
</form>
</body>
</html>


result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        ArrayList array = (ArrayList) session.getAttribute("array");
        String pages = (String) session.getAttribute("pages");
        int i = 0;
        while (i < array.size()) {

            out.print(array.get(i));
            i++;
        }
        int j = 1;

        while (j <= Integer.parseInt(pages) + 1) {

            out.println("<a href=result.jsp?id=" + j + ">" + j
                    + "</a>");

            j++;
        }
    %>
</body>
</html>


csvfile.java
/**
 *
 */
package doa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Boomiraj
 *
 */
public class CSVFile {
    public static ArrayList<String> getArrayList(ArrayList list) throws IOException{
        //FileWriter fw = new FileWriter("C:/Users/Boomiraj/Desktop/Boomiraj.csv");
       //fw.close();
       File file = new File("C:/Users/Boomiraj/Desktop/test.csv");
       FileReader fr = new FileReader(file);
       BufferedReader pr =new BufferedReader(fr);
       String dataRow = pr.readLine();
      //System.out.println(dataRow);
      // ArrayList array = new ArrayList();
       if(file.isFile()){
       System.out.println(dataRow);
       while (dataRow != null){
        String[] dataArray = dataRow.split(",");
        for (String item:dataArray) {
            //System.out.println(item);
          // pr.re(item+",");
            list.add(item);
           // System.out.println(list);
        }
       // System.out.println("test"); // Print the data line.
        dataRow = pr.readLine();
       fr.read();
     //  System.out.println(list);
       }}
       return list;
   }
    public static void main(String[] args) throws IOException {
        ArrayList list = new ArrayList();
        new CSVFile().getArrayList(list);
        /*FileWriter fw = new FileWriter("C:/Users/Boomiraj/Desktop/Boomiraj.csv");
           fw.close();*/
         /*  File file = new File("C:/Users/Boomiraj/Desktop/test.csv");
           FileReader fr = new FileReader(file);
           BufferedReader pr =new BufferedReader(fr);
           String dataRow = pr.readLine();
          // System.out.println(dataRow);
          ArrayList array = new ArrayList();
           if(file.isFile()){
           //System.out.println(dataRow);
           while (dataRow != null){
            String[] dataArray = dataRow.split(",");
            for (String item:dataArray) {
                //System.out.println(item);
              // pr.re(item+",");
                array.add(item);
               // System.out.println(array);
            }
           // System.out.println();
            //System.out.println("test"); // Print the data line.
            dataRow = pr.readLine();
           fr.read();
           System.out.println(array);
           }}*/
    }

}



csvservlet.java
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.CSVBean;
import doa.CSVFile;
import doa.CSVFileReadWrite;

/**
 * Servlet implementation class CSVLoginServlet
 */
public class CSVLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CSVLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /*PrintWriter out = response.getWriter();
        CSVBean csvBean = new CSVBean();
        csvBean.setFirstName(request.getParameter("firstname"));
        csvBean.setLastName(request.getParameter("lastname"));
        csvBean.setPhoneNumber(request.getParameter("phonenumber"));
       
        CSVFileReadWrite.writeFile(csvBean);
        out.println("Write Finished");
        */
       
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        CSVFile csv = new CSVFile();
        //csv.getArrayList();
        ArrayList array = new ArrayList();
        HttpSession session = request.getSession(true);
        String pageNo = request.getParameter("id");
        //System.out.println(csv.getArrayList(array));
        if(pageNo==null){
            pageNo="1";
        }
        final int offSet = 2;

        int pageNumber = Integer.parseInt(pageNo);
        if(pageNumber != 0){
        //System.out.println("67"+array.add(csv.getArrayList(array)));
        int numberOfData = array.size();
        //System.out.println(array);
        int totalPages = numberOfData/offSet;
        String noPages = Integer.toString(totalPages);
        System.out.println(noPages + 1);
        int nodata = offSet*pageNumber;

        int fistNo = nodata - offSet;
        //System.out.println("NO Of Data"+nodata);
        //System.out.println("From"+fistNo+"To"+nodata);

        if(nodata>array.size()){
            nodata = array.size();
        }
        ArrayList onlyData = new ArrayList();
        while (fistNo<nodata ){
            //StudentBean student = (StudentBean) arrayList.get(fistNo);
            //System.out.println(student.getName_Strudet());

            array.get(fistNo);
           
            fistNo ++;
        }
        }
        System.out.println("servlet 88"+array);
        session.setAttribute("array", array);
        session.setAttribute("pages", pageNo);
        response.sendRedirect("result.jsp");
    }

}

Tuesday, January 15, 2013

get the arraylist value from servlet to jsp

Servlet:
PrintWriter out = response.getWriter();
        out.println("test");
        BufferedReader buffer = new BufferedReader(new FileReader(new File("C:/Users/boomiraj/Desktop/Boomiraj1.csv")));
        String readline = buffer.readLine();
        ArrayList<String> array = new ArrayList<String>();
       
        while(readline != null){
            String[] read = readline.split(" ");
            //out.println(read);
        array.add(read[0]);
        array.add(read[1]);
        array.add(read[2]);
        //array.add("sekar");
        Iterator<String> i = array.iterator();
        while(i.hasNext()){
            i.next();
        }
        readline = buffer.readLine();
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("test", array);
        RequestDispatcher rd;
        rd = request.getRequestDispatcher("checkout.jsp");
        rd.forward(request,response);
        //response.sendRedirect("");



JSP file checkout.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList<String> array =(ArrayList)session.getAttribute("test");
if(!array.isEmpty()){
    for(int i = 0; i<array.size(); i++){
        %>
        <td><%= array.get(i)%></td>
       
       
<%}%><br>
    <%}%>       
</body>
</html>

Monday, January 7, 2013

date

public static void noOfDaysInMonth(){
       
        System.out.println("Enter the month");
        Scanner scanner = new Scanner(System.in);
        String month = scanner.next();
        System.out.println("Enter the year");
        int year = scanner.nextInt();
        String[] months = {"january","febrary","march","april","may","june","july","aguest","septemper","october","november","december"};
        for(int i = 0; i<months.length;i++){
        if(month.equals(months[i])){
        switch(i){
        case 0:
            System.out.println("no of days 31");
            break;
        case 1:
            System.out.println("no of days 29");
            break;
        case 2:
            System.out.println("no of days 31");
            break;
        case 3:
            System.out.println("no of days 30");
            break;
        case 4:
            System.out.println("no of days 31");
            break;
        case 5:
            System.out.println("no of days 30");
            break;
        case 6:
            System.out.println("no of days 31");
            break;
        case 7:
            System.out.println("no of days 31");
            break;
        case 8:
            System.out.println("no of days 30");
            break;
        case 9:
            System.out.println("no of days 31");
            break;
        case 10:
            System.out.println("no of days 30");
            break;
        case 11:
            System.out.println("no of days 31");
            break;
           
        }
       
        }
        }
       
       
    }
    public static void listOfHolidays(){
        String[] JANUARY = {"New Year","Pongal","Republicday"};
        String[] FEBRARY = {"No Holiday in this month"};
        String[] MARCH = {"No Holiday in this month"};
        String[] APRIL = {"No Holiday in this month"};
        String[] MAY = {"LaboursDay"};
        String[] JUNE = {"No Holiday in this month"};
        String[] JULY = {"No Holiday in this month"};
        String[] AUGEST = {"Independents days"};
        String[] SEPDEMBER = {"Diwali"};
        String[] OCTOBER = {"No Holiday in this month"};
        String[] NOVEMBER = {"Ramjan"};
        String[] DECEMBER = {"Christmas"};
       
        System.out.println("enter the month");
        Scanner scanner = new Scanner(System.in);
        String month = scanner.next();
        if(month.equalsIgnoreCase("JANUARY")){
            for(int i = 0; i<JANUARY.length;i++)
            System.out.println(JANUARY[i]);
           
        }
    }