我有一个简单的 Java 程序,它可以读取文件并将其写入 Eclipse 工具的控制台上。我正在尝试执行相同的 Java 程序来读取远程 Linux 服务器上的文件。我怎样才能做到这一点?
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args){
String path = "C:/tmp";
String file = "java2502201411.txt";
try
{
FileInputStream in = new FileInputStream(path + "/" + file);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine())!= null)
{
System.out.println(strLine);
}
}catch(Exception e){
System.out.println(e);
}
}
}
我可以从网络中的另一个 Windows 环境调用文件。例如:我正在尝试读取 Windows 网络上的文件,例如 usmnp1804 终端。
String path = "//usmnp1804/C$/tmp"
。同样,我尝试读取 Linux 服务器上的文件,但它没有按预期工作,出现诸如“文件未找到异常”之类的错误。我怀疑我的 Java 程序无法访问 Linux 服务器路径。
String path = "/home/jctadm/tmp".
似乎是事实上的标准。 Apache Commons Virtual File System 也支持此功能。两者都有标签:jcifs 和 apache-commons-vfs。