SlideShare ist ein Scribd-Unternehmen logo
1 von 11
fileCopy.jsp<br /><%@page contentType=quot;
text/htmlquot;
 pageEncoding=quot;
UTF-8quot;
%><br /><!DOCTYPE HTML PUBLIC quot;
-//W3C//DTD HTML 4.01 Transitional//ENquot;
<br />   quot;
http://www.w3.org/TR/html4/loose.dtdquot;
><br /><%<br />    String message = request.getParameter(quot;
messagequot;
);<br />    if(message == null)<br />        message = quot;
quot;
;<br />%><br /><html><br /> <head><br /> <meta http-equiv=quot;
Content-Typequot;
 content=quot;
text/html; charset=UTF-8quot;
><br /><title>JSP Page</title><br /> </head><br /> <body><br /> <br/><br/><br/><br/><br /><form name=quot;
frmquot;
 action=quot;
./copyFilesAction.jspquot;
 method=quot;
postquot;
><br /><table align=quot;
centerquot;
><br /><tr align=quot;
centerquot;
><br /><td colspan=quot;
2quot;
><h2>Copy Files</h2></td><br /><td></td><br /></tr><br /><tr><br /><td>Source</td><br /><td><input type=quot;
textquot;
 name=quot;
sourcequot;
 id=quot;
scquot;
/></td><br /><td><font>ex.- C:/SourceFolder </font></td><br /></tr><br /><tr><br /><td>Destination</td><br /><td><input type=quot;
textquot;
 name=quot;
destinationquot;
 id=quot;
dtquot;
/></td><br /><td><font>ex.- C:/DestinationFolder </font></td><br /></tr><br /><tr><br /><td></td><br /><td><input type=quot;
submitquot;
 value=quot;
Submitquot;
/><br /><input type=quot;
resetquot;
 value=quot;
Cancelquot;
/><br /></td><br /></tr><br /><tr><br /><td colspan=quot;
3quot;
 align=quot;
centerquot;
><%=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=quot;
javaquot;
 contentType=quot;
text/html; charset=ISO-8859-1quot;
<br />pageEncoding=quot;
UTF-8quot;
%><br /><%@page import=quot;
java.io.Filequot;
%><br /><%@page import=quot;
java.io.Filequot;
%><br /><%@page import=quot;
java.util.ArrayListquot;
%><br /><%<br />String source = request.getParameter(quot;
sourcequot;
);<br />String destination = request.getParameter(quot;
destinationquot;
);<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 == quot;
quot;
 || destination ==  quot;
quot;
){<br />message = quot;
Neither  of the fields can be balnk.quot;
;<br />response.sendRedirect(quot;
./copyFiles.jsp?message=quot;
+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 = quot;
The specified foders should exist on the system.quot;
;<br />response.sendRedirect(quot;
./copyFiles.jsp?message=quot;
+message);<br />}else{<br />//checking same source and destination<br />if(source.equals(destination)){<br />message = quot;
The source and destination folder can’t be samequot;
;<br />response.sendRedirect(quot;
./copyFiles.jsp?message=quot;
+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 = quot;
&lt;FileCount><br/>&lt;DOC><br/>quot;
+countDOC+quot;
<br/> &lt;/DOC><br/> &lt;RTF><br/>quot;
+countRTF+quot;
<br/> &lt;/RTF><br/> &lt;PDF><br/>quot;
+countPDF+quot;
<br/> &lt;/PDF><br/> &lt;Total><br/>quot;
+total+quot;
<br/> &lt;/Total><br/> &lt;/FileCount>quot;
;<br />//displaying required output to the user<br />out.println(xmlFormat);<br />out.println(quot;
<br/><br/><br/>quot;
+countDOC+quot;
= Total no of .doc file found<br/>quot;
);<br />out.println(countRTF+quot;
= Total no of .rtf file found<br/>quot;
);<br />out.println(countPDF+quot;
= Total no of .pdf file foundquot;
);<br />//==============================================<br />}else{<br />//<br />message = quot;
One can’t be a subfolder of the otherquot;
;<br />response.sendRedirect(quot;
./copyFiles.jsp?message=quot;
+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(quot;
.quot;
);<br />String fileExtension = fileName.substring(lIndex+1);<br />System.out.println(fileExtension);<br />//if it is a doc file<br />if(fileExtension.equals(quot;
docquot;
)){<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(quot;
rtfquot;
)){<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(quot;
pdfquot;
)){<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+quot;
/DOCquot;
);<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+quot;
/DOC/quot;
+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+quot;
/DOC/quot;
+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+quot;
/RTFquot;
);<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+quot;
/RTF/quot;
+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+quot;
/RTF/quot;
+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 />
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming
java programming

Weitere ähnliche Inhalte

Was ist angesagt? (11)

Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Introduction to php
Introduction  to  phpIntroduction  to  php
Introduction to php
 
Uplift – Generating RDF datasets from non-RDF data with R2RML
Uplift – Generating RDF datasets from non-RDF data with R2RMLUplift – Generating RDF datasets from non-RDF data with R2RML
Uplift – Generating RDF datasets from non-RDF data with R2RML
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
An Introduction to SPARQL
An Introduction to SPARQLAn Introduction to SPARQL
An Introduction to SPARQL
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scraping
 
2008 11 13 Hcls Call
2008 11 13 Hcls Call2008 11 13 Hcls Call
2008 11 13 Hcls Call
 
Ks2008 Semanticweb In Action
Ks2008 Semanticweb In ActionKs2008 Semanticweb In Action
Ks2008 Semanticweb In Action
 
Po sm
Po smPo sm
Po sm
 
SWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDFSWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDF
 
Xpsupport
XpsupportXpsupport
Xpsupport
 

Ähnlich wie java programming

Ähnlich wie java programming (20)

Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
File upload for the 21st century
File upload for the 21st centuryFile upload for the 21st century
File upload for the 21st century
 
Ch3(working with file)
Ch3(working with file)Ch3(working with file)
Ch3(working with file)
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
The FPDF Library
The FPDF LibraryThe FPDF Library
The FPDF Library
 
PHP
PHP PHP
PHP
 
Files
FilesFiles
Files
 
Files
FilesFiles
Files
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload Mysql
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
 
File in C language
File in C languageFile in C language
File in C language
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
Railson dickinson 5 mil seguidores
Railson dickinson 5 mil seguidoresRailson dickinson 5 mil seguidores
Railson dickinson 5 mil seguidores
 
File handling-c
File handling-cFile handling-c
File handling-c
 

Kürzlich hochgeladen

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Kürzlich hochgeladen (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

java programming

  • 1. fileCopy.jsp<br /><%@page contentType=quot; text/htmlquot; pageEncoding=quot; UTF-8quot; %><br /><!DOCTYPE HTML PUBLIC quot; -//W3C//DTD HTML 4.01 Transitional//ENquot; <br /> quot; http://www.w3.org/TR/html4/loose.dtdquot; ><br /><%<br /> String message = request.getParameter(quot; messagequot; );<br /> if(message == null)<br /> message = quot; quot; ;<br />%><br /><html><br /> <head><br /> <meta http-equiv=quot; Content-Typequot; content=quot; text/html; charset=UTF-8quot; ><br /><title>JSP Page</title><br /> </head><br /> <body><br /> <br/><br/><br/><br/><br /><form name=quot; frmquot; action=quot; ./copyFilesAction.jspquot; method=quot; postquot; ><br /><table align=quot; centerquot; ><br /><tr align=quot; centerquot; ><br /><td colspan=quot; 2quot; ><h2>Copy Files</h2></td><br /><td></td><br /></tr><br /><tr><br /><td>Source</td><br /><td><input type=quot; textquot; name=quot; sourcequot; id=quot; scquot; /></td><br /><td><font>ex.- C:/SourceFolder </font></td><br /></tr><br /><tr><br /><td>Destination</td><br /><td><input type=quot; textquot; name=quot; destinationquot; id=quot; dtquot; /></td><br /><td><font>ex.- C:/DestinationFolder </font></td><br /></tr><br /><tr><br /><td></td><br /><td><input type=quot; submitquot; value=quot; Submitquot; /><br /><input type=quot; resetquot; value=quot; Cancelquot; /><br /></td><br /></tr><br /><tr><br /><td colspan=quot; 3quot; align=quot; centerquot; ><%=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=quot; javaquot; contentType=quot; text/html; charset=ISO-8859-1quot; <br />pageEncoding=quot; UTF-8quot; %><br /><%@page import=quot; java.io.Filequot; %><br /><%@page import=quot; java.io.Filequot; %><br /><%@page import=quot; java.util.ArrayListquot; %><br /><%<br />String source = request.getParameter(quot; sourcequot; );<br />String destination = request.getParameter(quot; destinationquot; );<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 == quot; quot; || destination == quot; quot; ){<br />message = quot; Neither of the fields can be balnk.quot; ;<br />response.sendRedirect(quot; ./copyFiles.jsp?message=quot; +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 = quot; The specified foders should exist on the system.quot; ;<br />response.sendRedirect(quot; ./copyFiles.jsp?message=quot; +message);<br />}else{<br />//checking same source and destination<br />if(source.equals(destination)){<br />message = quot; The source and destination folder can’t be samequot; ;<br />response.sendRedirect(quot; ./copyFiles.jsp?message=quot; +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 = quot; &lt;FileCount><br/>&lt;DOC><br/>quot; +countDOC+quot; <br/> &lt;/DOC><br/> &lt;RTF><br/>quot; +countRTF+quot; <br/> &lt;/RTF><br/> &lt;PDF><br/>quot; +countPDF+quot; <br/> &lt;/PDF><br/> &lt;Total><br/>quot; +total+quot; <br/> &lt;/Total><br/> &lt;/FileCount>quot; ;<br />//displaying required output to the user<br />out.println(xmlFormat);<br />out.println(quot; <br/><br/><br/>quot; +countDOC+quot; = Total no of .doc file found<br/>quot; );<br />out.println(countRTF+quot; = Total no of .rtf file found<br/>quot; );<br />out.println(countPDF+quot; = Total no of .pdf file foundquot; );<br />//==============================================<br />}else{<br />//<br />message = quot; One can’t be a subfolder of the otherquot; ;<br />response.sendRedirect(quot; ./copyFiles.jsp?message=quot; +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(quot; .quot; );<br />String fileExtension = fileName.substring(lIndex+1);<br />System.out.println(fileExtension);<br />//if it is a doc file<br />if(fileExtension.equals(quot; docquot; )){<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(quot; rtfquot; )){<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(quot; pdfquot; )){<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+quot; /DOCquot; );<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+quot; /DOC/quot; +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+quot; /DOC/quot; +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+quot; /RTFquot; );<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+quot; /RTF/quot; +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+quot; /RTF/quot; +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 />