Solved

moving file from one folder to another

Posted on 2001-06-22
8
210 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
advertisement module in core php 4 148
Java Server Faces parameter pass? 6 39
object oriented programming comparison 5 57
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 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

19 Experts available now in Live!

Get 1:1 Help Now