views:

80

answers:

4

How to copy a file from Linux System to Windows system using Java program? Thanks for your help. I want to copy file from linux : /inet/apps/test.java to windows System1: C:\apps\test We can use following program to copy

    public static void copyFiles(String fromFile, String toFile ){
     FileInputStream from = null;
        FileOutputStream to = null;
        try {
          from = new FileInputStream(fromFile);
          to = new FileOutputStream(toFile);
          byte[] buffer = new byte[4096];
          int bytesRead;

          while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
        }catch(Exception e){e.printStackTrace();}
        finally {
          if (from != null)
            try { from.close();} catch (IOException e) {}
          if (to != null)try {to.close();} catch (IOException e) {}
        }     
 }

This program is running on linux. so fromFile = /inet/apps/test. What will be the toFile path. If i use simply C:\apps\test then how applicaiton recognise the target as System1.

+2  A: 

Java makes no diffeence between Windows and Linux files. So, as long as you have access to both filesystem in the computer your java program is running, you can just copy them.

Pablo Santa Cruz
I am running java file in linux,. So unixFile Path will be /inet/apps/test.java. How to specify Windows file >Sorry it may be basic.
vishnu
+1  A: 
  1. I think you are asking about some properties for the program.

    In that case the properties, should be configurable. You can keep the properties file in the same directory as your Java program or in the class path.

  2. The property file might look like :

        windows.filepath = C:\user\somefile.txt
        unix.filepath = /inet/apps/test.txt
    

    So when you port environments. You don't need to change the properties.

    If you are asking about how to port test.java to windows, then just copy the file to JAVA_HOME directory on windows and then you are good to go.

  3. Or If you have a Dual boot system. You can access your linux drive from windows, but not the other way around.

Vanchinathan Chandrasekaran
Thank you. if i use windows.filepath = C:\user\somefile.txt.in this way how application recognises the target system. Please see my edit in Question
vishnu
A: 

If the Unix system has the Window file system cross-mounted (e.g. via an SMB share), you should be able to find the Unix pathname that corresponds to the Windows destination and copy as you are currently doing.

Otherwise, you will need to use a file transfer protocol of some kind to copy the file.

There's no Java magic that allows you to magically write files to a different computer. The operating system has to be set up to allow this to happen.

FOLLOW UP - you asked:

I have no thought about the magic. So my question was how to copy a file from Windows to Linux. Normally we do FTP on unix Without mounting or we use FileZilla tool to transfer happens. Here if we want to do same thing though java then how to do that?

I don't know how I can say this differently to make you understand, but here goes:

Your choices in Java are basically the same:

  • You can use FTP. For example on the destination machine, turn pathname of the source file into a "ftp://..." URL, and use java.net.URL.connect() to pull it. There are probably 3rd-party Java libraries that you can use to "push" the file to a FTP server.
  • If your OS is setup with the file systems cross-mount, you can do a regular file copy, much as your code does.
  • You can use java.lang.System.exec(...) to run some Windows specific command line utility to do the copying.

In all cases, you will need to figure out how to map pathnames between the Windows and Linux worlds.

Stephen C
What is the meaning of " you will need to use a file transfer protocol of some kind to copy the file". Is this through UNIX box? I need to this through java program. Please help
vishnu
Are'nt the files on the same machine?
Vanchinathan Chandrasekaran
@Vanchinathan Chandrasekaran - If so, how would you explain the phrase: *"... then how applicaiton recognise the target as System1."* ? I think the root issue is that the OP *thinks* that Java can magically copy files from one machine to another.
Stephen C
I have no thought about the magic. So my question was how to copy a file from Windows to Linux. Normally we do FTP on unix Without mounting or we use FileZilla tool to transfer happens. Here if we want to do same thing though java then how to do that?
vishnu
I confused with the answer that, java does not have any difference between them then i expanded my question with IO transfer
vishnu
I haven't posted my question clearly with my confusion. Thank you for all of your time
vishnu
A: 

In Unix, we can do through ftp? Can we do the same thing through the java program?

vishnu
@vishnu - you can make a Java application send and receive files using the FTP protocol. For example, using `URL.connect` etcetera on a URL of the form `"ftp://dns-name/filepath"`. But the "other" machine has to be running an FTP server to allow this to happen.
Stephen C
@vishnu - by the way, it is considered poor etiquette to use an Answer to ask a followup question. Either edit your question, or post a comment on it or on an answer.
Stephen C
I dont want to post this as answer, but that time i have logged in another m/c and because of login problem i logged in different user which does not see option to edit my question. Sorry for this
vishnu
If we want to do transfer from UNIX to windows, FTP client has to run on Windows m/c. Can you give example for this?Thanks for your time. I am looking for this
vishnu