Solved

moving file from one folder to another

Posted on 2001-06-22
8
211 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 63
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
servlet example 17 32
hibernate example for saving data 19 43
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 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