Solved

Moving and copying files??

Posted on 2001-06-19
6
259 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 26
ejb stateless example 2 68
Java: The Public Class Main 4 66
Coding for the first time 9 100
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses

739 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