Solved

Moving and copying files??

Posted on 2001-06-19
6
253 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 50 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

820 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