Solved

moving file from one folder to another

Posted on 2001-06-22
8
209 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
Comment Utility
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
Comment Utility
10 points???
Home work
0
 

Author Comment

by:indupriya
Comment Utility
hai tonous,
how it works can you say the syntax.
reply
0
 
LVL 1

Expert Comment

by:tonus
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:r_sudip
Comment Utility
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
Comment Utility
hello sudip,
can you send me the code ,please.
reply
0
 
LVL 1

Expert Comment

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

Accepted Solution

by:
r_sudip earned 50 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now