How to copy a file from Linux System to Windows system using Java program?
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开发者_JS百科, 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
.
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.
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.
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.
Or If you have a Dual boot system. You can access your linux drive from windows, but not the other way around.
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.
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);
精彩评论