1

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.

1

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1
  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.

1 Comment

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
0

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.

6 Comments

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
Are'nt the files on the same machine?
@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.
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 confused with the answer that, java does not have any difference between them then i expanded my question with IO transfer
|
0

You can try the copy() method of the java.nio.file.Files class, of course we assume that your application can access both the path on Linux and Windows either by mapping the folders or something similar. For example

Files.copy(Paths.get(source), Paths.get(destination), StandardCopyOption.REPLACE_EXISTING, COPY_ATTRIBUTES);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.