java 读取 共享文件,如何从共享位置Windows读取文件? (Java)

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

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

}

}

And I execute this file using a windows batch runme.bat that has

java Sample

PAUSE

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.

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

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.

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..

Thanks

Veekay

解决方案

Two ways of doing.

1) Map the shared path to a local drive.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值