java 如何使用Java程序将文件从Linux系统复制到Windows系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3938264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:02:33  来源:igfitidea点击:

How to copy a file from Linux System to Windows system using Java program?

javafilesystems

提问by vishnu

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

如何使用Java程序将文件从Linux系统复制到Windows系统?谢谢你的帮助。我想将文件从 linux : /inet/apps/test.java 复制到 windows System1: C:\apps\test 我们可以使用以下程序进行复制

    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 toFilepath. If i use simply C:\apps\testthen how applicaiton recognise the target as System1.

这个程序在linux上运行。所以fromFile = /inet/apps/testtoFile路径是什么。如果我简单地使用,C:\apps\test那么应用程序如何将目标识别为System1.

回答by Pablo Santa Cruz

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.

Java 在 Windows 和 Linux 文件之间没有区别。因此,只要您可以访问正在运行的 Java 程序的计算机中的两个文件系统,您就可以复制它们。

回答by Vanchinathan Chandrasekaran

  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. 我认为您是在询问该程序的某些属性。

    在这种情况下,属性应该是可配置的。您可以将属性文件保存在与 Java 程序相同的目录中或类路径中。

  2. 属性文件可能如下所示:

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

    所以当你移植环境时。您不需要更改属性。

    如果您询问如何将 test.java 移植到 windows,那么只需将文件复制到 windows 上的 JAVA_HOME 目录,然后就可以了。

  3. 或者如果您有双引导系统。您可以从 windows访问您的linux 驱动器,但反过来不行。

回答by Stephen C

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.

如果 Unix 系统具有交叉安装的 Window 文件系统(例如,通过 SMB 共享),您应该能够找到与 Windows 目标相对应的 Unix 路径名,并像您当前所做的那样进行复制。

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.

没有 Java 魔法可以让您神奇地将文件写入不同的计算机。必须设置操作系统以允许这种情况发生。

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?

我没有想过魔法。所以我的问题是如何将文件从 Windows 复制到 Linux。通常我们在 unix 上做 FTP 没有挂载或者我们使用 FileZilla 工具进行传输。在这里,如果我们想通过 java 做同样的事情,那么该怎么做呢?

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:

您在 Java 中的选择基本相同:

  • 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.
  • 您可以使用 FTP。例如在目标机器上,将源文件的路径名转换为“ftp://...” URL,并用于java.net.URL.connect()拉取它。可能有第 3 方 Java 库可用于将文件“推送”到 FTP 服务器。
  • 如果您的操作系统设置了文件系统交叉安装,您可以进行常规文件复制,就像您的代码所做的那样。
  • 您可以使用java.lang.System.exec(...)运行某些特定于 Windows 的命令行实用程序来进行复制。

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

在所有情况下,您都需要弄清楚如何在 Windows 和 Linux 世界之间映射路径名。