如何从共享位置Windows读取文件? (Java) [英] How to Read file from a shared location Windows? (Java)

查看:249
本文介绍了如何从共享位置Windows读取文件? (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Windows上的网络共享位置读取文件?

Is there a way to read a file from a network shared location on windows?

例如,我有一个简单的代码,该代码从 Addons 文件夹.

Let's say for instance I've this simple code that reads a text file called readMe.txt from the Addons folder.

import java.io.File;

class Sample{

    public static void main(String[] ar){
        File file = new File("Addons/ReadMe.txt");
        System.out.println(file.getAbsolutePath());
      //followed by printing the contents of file   
    }
}

然后我使用具有

java Sample
PAUSE

仅当我将带有ReadMe.txt,Sample.class,runme.bat文件的Addons文件夹放入本地驱动器时,bat才运行并执行上述类.

The bat runs and executes the above class only when I place the Addons folder with ReadMe.txt, Sample.class, runme.bat file in my local drive.

将其放置在具有UNC路径的网络共享位置时,例如 \\ name \ Shared

When it is placed in a network shared location with UNC path like \\name\Shared

在这种情况下,bat文件通常从 C:\ Windows 开始运行,并抛出classNotFoundException.我可以将共享驱动器映射到 * Z:* 驱动器或其他驱动器,但我不想这样做.

In such a scenario, the bat file typically starts the base from C:\Windows and throws a classNotFoundException. I can rather map the shared drive to a *Z:* drive or whatever but I do not want to do it.

我希望代码以编程方式检测和检索Addons文件夹中Readme.txt的内容,而不管它是在本地驱动器上还是在共享驱动器上执行.有没有办法做到这一点?请帮忙.

I want the code to programatically detect and retrieve the content of Readme.txt in Addons folder irrespective of whether it is being executed on a local drive or on a shared drive. Is there a way to achieve this? Please help..

谢谢

Veekay

推荐答案

两种方法.

1)将共享路径映射到本地驱动器.

1) Map the shared path to a local drive.

2)如Medopal所述,将服务器路径硬编码到新的File('')中.

2) Else hard code the server path in new File('') as mentioned by Medopal.

在本地系统上执行时,类似new File("").getAbsolutePath()的内容可能会帮助我获取基本Folder.同样,当在共享位置执行时,也没有这样的方式以编程方式找到工作基地.

Something like new File("").getAbsolutePath() might help me get the base Folder when executed on a local system. Likewise there is no such way to programmatically find out the working base when executed on a shared location.

这篇关于如何从共享位置Windows读取文件? (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆