如何使用
java获取
Windows PC上的文件或目录的网络路径?通常我们可以在Windows上的共享文件夹属性中看到它.如下所示….. 最佳答案 使用
Java,您可以在Runtime类的exec方法中传递net SHARE命令,然后解析Process类的输出流以获取目录的相应网络路径. net SHARE directory_name命令的输出如下:
Share name Share
Path C:\Share
Remark
Maximum users No limit
Users
Caching Manual caching of documents
Permission user, FULL
您需要从上面的输出中获取Path键的值.以下是如何实现此目的的伪代码:
Process process = Runtime.getRuntime().exec("net SHARE directory_name");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringWriter writer = new StringWriter();
String line;
while (null != (line = reader.readLine())) {
writer.write(line);
}
System.out.println(writer.toString());