fileCopy.jsp<br /><%@page contentType="text/html" pageEncoding="UTF-8"%><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming
Upcoming SlideShare
Loading in...5
×

java programming

392

Published on

Plse have a look on this

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
392
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "java programming"

  1. 1. fileCopy.jsp<br /><%@page contentType="text/html" pageEncoding="UTF-8"%><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<br /> "http://www.w3.org/TR/html4/loose.dtd"><br /><%<br /> String message = request.getParameter("message");<br /> if(message == null)<br /> message = "";<br />%><br /><html><br /> <head><br /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /><title>JSP Page</title><br /> </head><br /> <body><br /> <br/><br/><br/><br/><br /><form name="frm" action="./copyFilesAction.jsp" method="post"><br /><table align="center"><br /><tr align="center"><br /><td colspan="2"><h2>Copy Files</h2></td><br /><td></td><br /></tr><br /><tr><br /><td>Source</td><br /><td><input type="text" name="source" id="sc"/></td><br /><td><font>ex.- C:/SourceFolder </font></td><br /></tr><br /><tr><br /><td>Destination</td><br /><td><input type="text" name="destination" id="dt"/></td><br /><td><font>ex.- C:/DestinationFolder </font></td><br /></tr><br /><tr><br /><td></td><br /><td><input type="submit" value="Submit"/><br /><input type="reset" value="Cancel"/><br /></td><br /></tr><br /><tr><br /><td colspan="3" align="center"><%=message%></td><br /><td></td><br /></tr><br /></table><br /></form><br /></body><br /></html><br />copyFileAction.jsp<br /><%--<br />File:- copyFileAction.jsp<br />Details :- It performs serverside validation and calls the appropriate methods for searching files on source and write to destinatinon<br />--%><br /><%@ page language="java" contentType="text/html; charset=ISO-8859-1"<br />pageEncoding="UTF-8"%><br /><%@page import="java.io.File"%><br /><%@page import="java.io.File"%><br /><%@page import="java.util.ArrayList"%><br /><%<br />String source = request.getParameter("source");<br />String destination = request.getParameter("destination");<br />String message = null;<br />File sFile = null;<br />File dFile = null;<br />//Checking fields are empty or not<br />if(source == null || destination == null || source == "" || destination == ""){<br />message = "Neither of the fields can be balnk.";<br />response.sendRedirect("./copyFiles.jsp?message="+message);<br />}else{<br />sFile = new File(source);<br />dFile = new File(destination);<br />//checking specified folder exist or not on the system<br />if(!(sFile.exists() && dFile.exists() && sFile.isDirectory() && dFile.isDirectory())){<br />message = "The specified foders should exist on the system.";<br />response.sendRedirect("./copyFiles.jsp?message="+message);<br />}else{<br />//checking same source and destination<br />if(source.equals(destination)){<br />message = "The source and destination folder can’t be same";<br />response.sendRedirect("./copyFiles.jsp?message="+message);<br />}else{<br />String sPath = sFile.getAbsolutePath();<br />String dPath = dFile.getAbsolutePath();<br />int i = sPath.indexOf(dPath);<br />int j = dPath.indexOf(sPath);<br />//checking existance of subfolder<br />if( i== -1 && j== -1){<br />//do the action here if one is not the subfolder of other============================<br />//package:- copyFiles //class:- CopyFiles<br />copyFiles.CopyFiles cf = new copyFiles.CopyFiles();<br />//it performs required search , read on the source and write to the destination<br />cf.search(sFile, destination, 0, 0, 0);<br />//countFile() returns an arrayList object containing no. of diff. files<br />ArrayList<Integer> count = cf.countFile();<br />int countDOC = count.get(0);<br />int countPDF = count.get(1);<br />int countRTF = count.get(2);<br />int total = countDOC + countPDF + countRTF;<br />String xmlFormat = "&lt;FileCount><br/>&lt;DOC><br/>"+countDOC+"<br/> &lt;/DOC><br/> &lt;RTF><br/>"+countRTF+"<br/> &lt;/RTF><br/> &lt;PDF><br/>"+countPDF+"<br/> &lt;/PDF><br/> &lt;Total><br/>"+total+"<br/> &lt;/Total><br/> &lt;/FileCount>";<br />//displaying required output to the user<br />out.println(xmlFormat);<br />out.println("<br/><br/><br/>"+countDOC+"= Total no of .doc file found<br/>");<br />out.println(countRTF+"= Total no of .rtf file found<br/>");<br />out.println(countPDF+"= Total no of .pdf file found");<br />//==============================================<br />}else{<br />//<br />message = "One can’t be a subfolder of the other";<br />response.sendRedirect("./copyFiles.jsp?message="+message);<br />}<br />}<br />}<br />}<br />%><br />copyFiles.java<br />/**<br />* File:-CopyFiles.java<br />* Details : It creates files on destination.<br />*/<br />package copyFiles;<br />import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileOutputStream;<br />import java.util.ArrayList;<br />public class CopyFiles {<br />int totalDOC;<br />int totalRTF;<br />int totalPDF;<br />/*public static void main(String[] args) {<br />// TODO Auto-generated method stub<br />}*/<br />public void search(File sFile, String destination, int doc, int rtf, int pdf){<br />File fileFolder[] = sFile.listFiles();<br />int i = 0;<br />//counter for doc file<br />totalDOC = doc;<br />//counter for rtf file<br />totalRTF = rtf;<br />//counter for pdf file<br />totalPDF = pdf;<br />try{<br />//looping through the all files and folder<br />for(i=0; i<fileFolder.length; i++){<br />if(fileFolder[i].isFile() == true){<br />String fileName = fileFolder[i++++++++++].getName();<br />int lIndex = fileName.lastIndexOf(".");<br />String fileExtension = fileName.substring(lIndex+1);<br />System.out.println(fileExtension);<br />//if it is a doc file<br />if(fileExtension.equals("doc")){<br />//create doc file on destination<br />createDOC(fileFolder[i],destination );<br />totalDOC++;<br />}else<br />//if it is a rtf file<br />if(fileExtension.equals("rtf")){<br />//create rtf file on destination<br />createRTF(fileFolder[i],destination );<br />totalRTF++;<br />}else<br />//if it is a pdf file<br />if(fileExtension.equals("pdf")){<br />totalPDF++;<br />//create pdf file on destination<br />createDOC(fileFolder[i],destination );<br />createRTF(fileFolder[i],destination );<br />}<br />//System.out.println(fileFolder[i].getName());<br />}else<br />if(fileFolder[i].isDirectory() == true){<br />//recursive call for subfolders<br />search(fileFolder[i], destination, totalDOC, totalRTF, totalPDF);<br />}<br />}<br />}catch(Exception e){<br />e.printStackTrace();<br />}<br />}<br />void createDOC(File searchFile, String destination) throws Exception{<br />File fDOC = new File(destination+"/DOC");<br />if(fDOC.exists()){<br />//if folder exists create file inside it<br />byte []inByte = new byte[(int)searchFile.length()];<br />FileInputStream fInStream = new FileInputStream(searchFile);<br />File fOut = new File(destination+"/DOC/"+searchFile.getName());<br />FileOutputStream fOutStream = new FileOutputStream(fOut);<br />fInStream.read(inByte);<br />fOutStream.write(inByte);<br />fInStream.close();<br />fOutStream.close();<br />}else{<br />//if folder not exist create it and also create file inside created folder<br />fDOC.mkdir();<br />byte []inByte = new byte[(int)searchFile.length()];<br />FileInputStream fInStream = new FileInputStream(searchFile);<br />File fOut = new File(destination+"/DOC/"+searchFile.getName());<br />FileOutputStream fOutStream = new FileOutputStream(fOut);<br />fInStream.read(inByte);<br />fOutStream.write(inByte);<br />fInStream.close();<br />fOutStream.close();<br />}<br />}<br />void createRTF(File searchFile, String destination)throws Exception{<br />File fRTF = new File(destination+"/RTF");<br />if(fRTF.exists()){<br />//if folder exists create file inside it<br />byte []inByte = new byte[(int)searchFile.length()];<br />FileInputStream fInStream = new FileInputStream(searchFile);<br />File fOut = new File(destination+"/RTF/"+searchFile.getName());<br />FileOutputStream fOutStream = new FileOutputStream(fOut);<br />fInStream.read(inByte);<br />fOutStream.write(inByte);<br />fInStream.close();<br />fOutStream.close();<br />}else{<br />//if folder not exist create it and also create file inside created folder<br />fRTF.mkdir();<br />byte []inByte = new byte[(int)searchFile.length()];<br />FileInputStream fInStream = new FileInputStream(searchFile);<br />File fOut = new File(destination+"/RTF/"+searchFile.getName());<br />FileOutputStream fOutStream = new FileOutputStream(fOut);<br />fInStream.read(inByte);<br />fOutStream.write(inByte);<br />fInStream.close();<br />fOutStream.close();<br />}<br />}<br />//it returns an ArrayList object containing diff. file counts<br />public ArrayList<Integer> countFile(){<br />ArrayList<Integer> count = new ArrayList<Integer>();<br />count.add(totalDOC);<br />count.add(totalPDF);<br />count.add(totalRTF);<br />return count;<br />}<br />}<br />

×