Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Moving and copying files??

Posted on 2001-06-19
6
Medium Priority
?
275 Views
Last Modified: 2010-03-31
How can I move or copy a file from one directory to another.. ??
0
Comment
Question by:simlox
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:jerch
ID: 6208247
Use renameTo method of File to move a file and use FileInputStream and FileOutputStream to copy files.
0
 
LVL 1

Accepted Solution

by:
rajendra_rathod earned 200 total points
ID: 6209208
import java.io.*;
import java.util.zip.*;
import java.text.*;

import java.util.*;

public class FileManipulation
{
/**
    * The below methos will copy a user mentioned file in the user defined folder
     * @param argInputFileName,passes the absolute path of the file to be moved
    * @param argOutPutDest,passes the absolute path of the the destination folder
     * return boolean showing the status
     * throws IOException
 */
public static boolean flag = false;

public static boolean copyFile(String argInputFileName,String argOutPutDest)throws IOException
          {
               try
               {
                    File deleteFile=new File(argInputFileName);
                    String parentfolder=deleteFile.getParent();
                    int lengthvalue=parentfolder.length();
                    String filename=argInputFileName.substring(lengthvalue);
                    FileInputStream l_input = new FileInputStream(argInputFileName);
                    File l_target = new File(argOutPutDest+"\\"+filename);
                    FileOutputStream l_out = new FileOutputStream(l_target);
                    int b;
                    while( (b = l_input.read())!= (-1) )
                    {
                         l_out.write(b);
                         flag=true;
                    }
                    l_out.close();
                    l_input.close();
               }catch(IOException ex)
                {
                    System.out.println("Error"+ex.getMessage());
                }
                   
               return flag;

          }



/**
    * The below methos will save a user mentioned file with the user defined content
     * @param fileName,passes the absolute path of the file to be created and saved
    * @param contents,passes the contents of the file
     * return boolean showing the status
     * throws IOException
 */
          public static boolean saveFile(String fileName,String contents)throws IOException
          {
                if((fileName.trim().equals(""))||(contents.trim().equals("")))
                {
                      flag=false;
                }
               
                if(fileName != null)
               {                                  
                    try
                    {
                         FileOutputStream out = new FileOutputStream(fileName);    
                         byte[] buffer = new byte[contents.length()];                              
                         buffer = contents.getBytes();                              
                         out.write(buffer);                                  
                         out.close();                                        
                          flag= true;
                    }
                    catch(IOException e)
                    {                                  
                         flag= false;
                         System.out.println("Error"+e.getMessage());
                    }

               }// end if()
               return flag;
          }
     }
0
 

Author Comment

by:simlox
ID: 6212176
brilliant... thanks a lot
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:simlox
ID: 6212178
brilliant... thanks a lot
0
 
LVL 1

Expert Comment

by:rajendra_rathod
ID: 6212898
Hi,

Thank u for appritiating. U can write the same code for moving the file as copy a file. only extra thing u have to do is after copying the file delete from the source folder.

Hope i will get good grade.
0
 

Author Comment

by:simlox
ID: 6213466
yeh... I've done just that.. thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

885 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