Solved

moving file from one folder to another

Posted on 2001-06-22
8
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 video teaches viewers about errors in exception handling.

724 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