Solved

Moving and copying files??

Posted on 2001-06-19
6
239 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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