Solved

Copy folders from computer A to computer B

Posted on 2003-11-03
12
470 Views
Last Modified: 2008-02-01
I need to write a program to achieve the following task
a) Copy a folder with all the files in it from computer A to computer B
b) Delete the folder from original computer
c) This java program must be able to run in any computers in the LAN.

What classes and methods do I need to achieve the task?
Does anyone has a sample program that copy file from one computer to another?
Please guide me how to do the task. Thanks
0
Comment
Question by:ricjava
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 3

Expert Comment

by:applekanna
ID: 9676391
Java RMI is the way to go
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9676397
A simple java RMI file transfer application with source code.

http://developer.java.sun.com/developer/technicalArticles/RMI/rmi_corba/

0
 
LVL 3

Expert Comment

by:applekanna
ID: 9676410
we can also use sockets as suggested by CEHJ in the thread

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20752924.html

>>b) Delete the folder from original computer
just add fileObject.delete() in the programs to delte the files

c) This java program must be able to run in any computers in the LAN.
>> JAVA must be installed on the client computers on the LAN

Hope this helps , Cheers !
0
 

Author Comment

by:ricjava
ID: 9676455
Hi expert,

Anymore help....please....why must I use rmi or socket and not the usual file operation?



0
 
LVL 3

Expert Comment

by:applekanna
ID: 9676469
>>must I use rmi or socket and not the usual file operation

Here we are talking about moving data from 1 copm to another as per your q, this invoices a network and communication of data over n/w so we use RMI or sockets.

I am not sure if we can do otherwise if there is I wold also like to know :)
We can also connect to it  using FTP if you have a ftp server running but again this involves sockets

Does this help?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 9676768
I will mail you an example.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Expert Comment

by:gnoon
ID: 9676791
but don't know your email. mail me if you still want the code.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 9676793
but don't know your email. mail me[nopadolsiri@hotmail.com] if you still want the code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9677622
Please do not mail anything anywhere. This is against the Member Agreement. Post the code here if you've got it.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 60 total points
ID: 9680744
Here's the code:

public static void copyFiles() {
      String TARGET_MACHINE_STUB = "\\\\machine_name\\share_name\\";
      File sourceDirectory = new File("C:\\WINNT");
      // Get all source files
      String[] sourceFiles = sourceDirectory.list();
      for(int i = 0;i < sourceFiles.length;i++) {
            FileCopy.copy(sourceFiles[i], TARGET_MACHINE_STUB + sourceFiles[i]);
      }
}

/////// and this /////////////////////////////


// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
// Copyright (c) 1997 by David Flanagan
// This example is provided WITHOUT ANY WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for non-commercial purposes.
// For any commercial use, see http://www.davidflanagan.com/javaexamples

import java.io.*;

/**
 * This class is a standalone program to copy a file, and also defines a
 * static copy() method that other programs can use to copy files.
 **/
public class FileCopy {
  /** The main() method of the standalone program.  Calls copy(). */
  public static void main(String[] args) {
    if (args.length != 2)    // Check arguments
      System.err.println("Usage: java FileCopy <source file> <destination>");
    else {
      // Call copy() to do the copy, and display any error messages it throws.
      try { copy(args[0], args[1]); }
      catch (IOException e) { System.err.println(e.getMessage()); }
    }
  }

  /**
   * The static method that actually performs the file copy.
   * Before copying the file, however, it performs a lot of tests to make
   * sure everything is as it should be.
   */
  public static void copy(String from_name, String to_name) throws IOException{
    File from_file = new File(from_name);  // Get File objects from Strings
    File to_file = new File(to_name);
   
    // First make sure the source file exists, is a file, and is readable.
    if (!from_file.exists())
      abort("FileCopy: no such source file: " + from_name);
    if (!from_file.isFile())
      abort("FileCopy: can't copy directory: " + from_name);
    if (!from_file.canRead())
      abort("FileCopy: source file is unreadable: " + from_name);
   
    // If the destination is a directory, use the source file name
    // as the destination file name
    if (to_file.isDirectory())
      to_file = new File(to_file, from_file.getName());
   
    // If the destination exists, make sure it is a writeable file
    // and ask before overwriting it.  If the destination doesn't
    // exist, make sure the directory exists and is writeable.
    if (to_file.exists()) {
      if (!to_file.canWrite())
        abort("FileCopy: destination file is unwriteable: " + to_name);
      // Ask whether to overwrite it
      System.out.print("Overwrite existing file " + to_name + "? (Y/N): ");
      System.out.flush();
      // Get the user's response.
      BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
      String response = in.readLine();
      // Check the response.  If not a Yes, abort the copy.
      if (!response.equals("Y") && !response.equals("y"))
        abort("FileCopy: existing file was not overwritten.");
    }
    else {  
      // if file doesn't exist, check if directory exists and is writeable.
      // If getParent() returns null, then the directory is the current dir.
      // so look up the user.dir system property to find out what that is.
      String parent = to_file.getParent();  // Get the destination directory
      if (parent == null) parent = System.getProperty("user.dir"); // or CWD
      File dir = new File(parent);          // Convert it to a file.
      if (!dir.exists())
        abort("FileCopy: destination directory doesn't exist: " + parent);
      if (dir.isFile())
        abort("FileCopy: destination is not a directory: " + parent);
      if (!dir.canWrite())
        abort("FileCopy: destination directory is unwriteable: " + parent);
    }
   
    // If we've gotten this far, then everything is okay.
    // So we copy the file, a buffer of bytes at a time.
    FileInputStream from = null;  // Stream to read from source
    FileOutputStream to = null;   // Stream to write to destination
    try {
      from = new FileInputStream(from_file);  // Create input stream
      to = new FileOutputStream(to_file);     // Create output stream
      byte[] buffer = new byte[4096];         // A buffer to hold file contents
      int bytes_read;                         // How many bytes in buffer
      // Read a chunk of bytes into the buffer, then write them out,
      // looping until we reach the end of the file (when read() returns -1).
      // Note the combination of assignment and comparison in this while
      // loop.  This is a common I/O programming idiom.
      while((bytes_read = from.read(buffer)) != -1) // Read bytes until EOF
        to.write(buffer, 0, bytes_read);            //   write bytes
    }
    // Always close the streams, even if exceptions were thrown
    finally {
      if (from != null) try { from.close(); } catch (IOException e) { ; }
      if (to != null) try { to.close(); } catch (IOException e) { ; }
    }
  }

  /** A convenience method to throw an exception */
  private static void abort(String msg) throws IOException {
    throw new IOException(msg);
  }
}
0
 
LVL 16

Expert Comment

by:gnoon
ID: 9684089
Sorry experts. I am new for experts-exchange.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9758206
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 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

10 Experts available now in Live!

Get 1:1 Help Now