?
Solved

Moving and copying files??

Posted on 2001-06-19
6
Medium Priority
?
272 Views
Last Modified: 2010-03-31
How can I move or copy a file from one directory to another.. ??
0
Comment
Question by:simlox
[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
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

719 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