Solved

moving file from one folder to another

Posted on 2001-06-22
8
212 Views
Last Modified: 2010-03-31
hai every body,The Folderdirectory (filetransfer) contains 2 folders
 
 1.folder1
 2.folder2

our target is to send a file from one folder to another.
i.e when we are moving a file which exists in folder1 to folder2 ,the file should
   not be available to folder1, but it should be available in folder2 only.
in my code the file is available in both the folders,when i am deleting also.
the problem what i find is the file is still exists in fileinputstream object only
how to sort out this.



copyfile.html
---------------
<html>
<body bgColor="#defddd">
<pre>
<form action="http://localhost:8080/servlet/copyfile10" method="post">
Filename:             <input type=text name="fname" value="">

Select Source Folder: <select name="file">
<option value=folder1>folder1
<option value=folder2>folder2

</select>



                             Select Destination Folder:<select name="file1">
                             <option value=folder1>folder1
                             <option value=folder2>folder2
                           
                             </select>

<input type=submit value="Submit">
</form>
</pre>
</body>
</html>

copyfile.java
-------------

import java.io.*;
import java.io.File.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class copyfile10 extends HttpServlet
{
int i=0;
FileInputStream fin;
FileOutputStream fout;
PrintWriter pw;
File f;
public void init(ServletConfig sc)throws ServletException
{
try
{
super.init(sc);
}
catch(Exception e)
{
System.out.println(e);
}
}//init method end
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
try
{
 String fname=req.getParameter("fname");
 String file=req.getParameter("file");
 pw=res.getWriter();
 pw.println("<html><body bgColor=#fedcba>");
 pw.println("<pre>");
 pw.println("<center>"+"File Status");
 pw.print("<hr width=100>");
// open i/p file
 pw.println("File Name is:           "+fname);
 pw.println("<br><br>");
 pw.println("Source Folder Is:       "+file);
 pw.print("</center>");
 pw.print("</pre>");
 pw.print("</body>");
 pw.print("</html>");
try
{
f=new File("f:\\filetransfer\\"+file+"\\"+fname);
fin=new FileInputStream(f);
f.delete();
}
catch(FileNotFoundException e)
{
System.out.println("i/p file not found");
return;
}
//open o/p file
try
{
String file1=req.getParameter("file1");
 fout=new FileOutputStream("f:\\filetransfer\\"+file1+"\\"+fname);
 System.out.println(fout);
 pw.println("<html><body>");
 pw.print("<pre>");
 pw.print("<center>");
 pw.println("Destination Folder Is:     "+file1);
 pw.print("</center>");
 pw.print("</pre>");
 pw.print("</body></html>");
}
catch(FileNotFoundException e)
{
System.out.println("error opening o/p file");
return;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("copy file from to:");
return;
}
//copy file
try
{
do
{
i=fin.read();
if(i!=-1)
{
fout.write(i);
}
if(i==-1)
{
f.delete();
//System.out.println(i);
System.out.println("i/p file is:"+f);
System.out.println(f);
}
}while(i!=-1);

 pw.println("<html><body>");
 pw.println("<pre>");
 pw.println("Your File is  Succefully inserted");
 pw.println("</pre>");
 pw.println("</body></html>");
}
catch(IOException e)
{
System.out.println("file error");
}

fin.close();
fout.close();
}
}





0
Comment
Question by:indupriya
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:tonus
ID: 6217312
You got a easy method renameTo(File dest) in File class which moves a file from one folder to another.
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6217317
10 points???
Home work
0
 

Author Comment

by:indupriya
ID: 6217562
hai tonous,
how it works can you say the syntax.
reply
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Expert Comment

by:tonus
ID: 6217690
Here is a sample code which transfer one.txt from folder1 to folder2:


File f = new File ("f:\\filetransfer\\folder1\\one.txt");
 f.renameTo(new File("f:\\filetransfer\\folder2\\one.txt"));
0
 

Expert Comment

by:r_sudip
ID: 6246313
Hi,

 There could be various ways of achieving this.Here is
 one more.

 1. Read the contents of the first file.
 2. Create and Write the contents read in step 1 to a new
    file in the new location.
 3. Delete the first file from the original location.
    For this u can use the Runtime.exec  method to execute
    the OS command for deleting a file.

Please let me know if u need the exact code.



Sudip
0
 

Author Comment

by:indupriya
ID: 6261170
hello sudip,
can you send me the code ,please.
reply
0
 
LVL 1

Expert Comment

by:tonus
ID: 6261351
Indupriya,
   Did you tried the code which I posted above? It works fine.
0
 

Accepted Solution

by:
r_sudip earned 50 total points
ID: 6270349
Hi Indupriya,

 Try the following code

          try
         {
             File f = new File("source file name");
             int size = new Long(f.length()).intValue();
             byte[] data = new byte[size];  
             RandomAccessFile sFile = new RandomAccessFile(f,"r");
             sFile.read(data);
             RandomAccessFile dFile = new RandomAccessFile("new filename and location","rw");
             dFile.write(data);
             Runtime.getRuntime().exec("c:\\winnt\\system32\\cmd.exe /C del <original source filename>");
         }
          catch(Exception e){e.printStackTrace();}

 This example however will work on windows NT, you will need to modify the last line of code according to the OS.


Sudip
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question