debugger位置不对_如何不对文件路径进行硬编码(How to not hard-code a file path)

如何不对文件路径进行硬编码(How to not hard-code a file path)

我想使用此实现计算放入源包的文件的路径:

URL pathSource = this.getClass().getResource("saveItem.xml");

当我尝试创建一个新的文件,如下面的代码:

File xmlFile = new File(pathSource.toString());

我尝试使用它来创建这样的文档:

Document document = builder.parse(xmlFile);

这给了我java.io.FileNotFoundException 。 如何在不进行硬编码的情况下计算文件路径?

PS:我已经使用了pathSource.getPath()但它也不起作用。 我想使用类似的实现:

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));

PPS:结构如下:

I'd like to calculate the path of a file placed into Source Packages using this implementation:

URL pathSource = this.getClass().getResource("saveItem.xml");

When I try to create a new File like the code below:

File xmlFile = new File(pathSource.toString());

And I try to use it to create a document like this:

Document document = builder.parse(xmlFile);

This give me the java.io.FileNotFoundException. How can I calculate the file path without hard-coding?

PS: I already used pathSource.getPath() but it doesn't work either. I would like to use a similar implementation:

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));

PPS: The structure is the following:

原文:https://stackoverflow.com/questions/33504209

更新时间:2020-02-11 09:02

最满意答案

您无法将JAR文件中的资源作为File实例进行访问。 你只能得到一个InputStream 。

因此,以下行

File xmlFile = new File(pathSource.toString());

将无法正常工作,并且当稍后尝试读取它时,将抛出FileNotFoundException 。

假设您正在尝试使用DocumentBuilder解析XML文件,您可以使用parse(InputStream)方法:

try (InputStream stream = this.getClass().getResourceAsStream("saveItem.xml")) {

Document document = builder.parse(stream);

}

You can't access a resource that inside a JAR file as a File instance. You can only get an InputStream to it.

As such, the following line

File xmlFile = new File(pathSource.toString());

won't work properly and when an attempt is made to read it later, a FileNotFoundException will be thrown.

Assuming you're trying to parse a XML file using DocumentBuilder, you can use the parse(InputStream) method:

try (InputStream stream = this.getClass().getResourceAsStream("saveItem.xml")) {

Document document = builder.parse(stream);

}

2015-11-03

相关问答

你可能是这样的: if(System.Diagnostics.Debugger.IsAttached)

System.Diagnostics.Debugger.Break();

当然这仍然会被编译成一个版本构建。 如果你希望它的行为更像Debug对象,其中代码根本不存在于Release构建中,那么你可以这样做: [Conditional("DEBUG")]

void DebugBreak()

{

if(System.Diagnostics.Debugger.IsAttached)

...

您甚至可以考虑更进一步,将默认值存储在数据库中。 这将提供配置的灵活性和这些默认值的集中管理。 就个人而言,我可能经常采用的方法是在代码中配置我的配置,但能够通过在配置文件或Db中提供新值来覆盖默认值。 为什么我走这么远? 这样,我可以使用最少的配置进行部署,然后在需要时提供覆盖。 You might even consider going one step further and storing your defaults in the database. This will provide f

...

最终,您将无法绕过您无法控制的硬编码路径,例如网络共享路径。 您可以而且应该避免的是在文档中对这些路径进行硬编码。 相反,将它们转移到配置文件和/或环境变量(同样,它们将由配置文件控制,与.bashrc和类似文件一起控制)。 然后使用最简单的方法 network_share_path = Sys.getenv('NETWORK_SHARE_PATH',

stop('no network share path configured'))

...

Sandbox并不意味着没有用户选择就无法访问文件和文件夹。 正如它在App Sandbox in Depth文章中所述,您仍然可以访问容器目录。 要获取Application Support -directory的路径,无论何时使用Sandboxing,都应使用相同的代码。 + (NSString *)executableName

{

NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectFor

...

不需要对now变量进行“硬编码”,因此它总是引用相同的时间点。 datetime库中的now()函数返回一个datetime对象; 返回对象的值不会随时间而变化。 >>> import datetime

>>> import time

>>> x = datetime.datetime.now()

>>> x

datetime.datetime(2010, 8, 14, 16, 26, 6, 592441)

>>> time.sleep(5)

>>> x

datetime.datetime(201

...

我建议你在config / index / bootstrap中做这样的事情: define('ROOT_DIRECTORY', dirname(__FILE__).'/');

通过这种方式,当您需要从其他位置加载文件时,可以使所有路径相对于ROOT_DIRECTORY。 例如: require_once(ROOT_DIRECTORY . 'abc/xyz.php');

这将使文件包含更简单,并允许您将整个项目目录移动到另一个位置(例如生产服务器),而不会打破任何“路径”逻辑。 看到你的代码更

...

采取(主要)从我的回答如何获得JavaScript中的相对路径? 。 你有两个选择: 在JavaScript中构建一个包含所有环境特定设置的配置/首选项对象: var config = {

base: "<?php echo base_url(); ?>",

someOtherPref: 4

};

然后用config.base前缀。 您必须将配置对象放置在由PHP解析的位置; 标准选择在

HTML元素中。 别担心它的一个小配置对象,其内容可以在每个页面上更改,

...

直接的答案是“除非你需要能够存储几乎无限种类的价值,否则这是不赞成的”。 您需要两个以上的值吗? 如果没有,请考虑布尔值。 如果是,请考虑枚举。 如果您需要几乎无限数量的值,则可以使用String。 如果您选择为此目的使用字符串,您的程序仍然可以正常工作。 但是,请始终牢记,您为代码增加复杂性的每个地方,都会有更多出错的机会,并且在未来会带来可维护性挑战。 The direct answer is 'this is frowned upon unless you need the ability

...

将其分解为单独的配置文件。 首先,它会让你至少设置一些变量,如“DEBUG_MODE”,它将为你的测试环境切换出你的生产凭证。 如果您愿意,您可以选择不将单独的文件保存在版本控制下,或者在代码存储库中保留一个带有伪凭证的单独文件,以便用户必须提供自己的凭证而不必访问全局凭证。 Factor it out into a separate config file. For one, it'll let you at the very least set some variable like "DEBU

...

您无法将JAR文件中的资源作为File实例进行访问。 你只能得到一个InputStream 。 因此,以下行 File xmlFile = new File(pathSource.toString());

将无法正常工作,并且当稍后尝试读取它时,将抛出FileNotFoundException 。 假设您正在尝试使用DocumentBuilder解析XML文件,您可以使用parse(InputStream)方法: try (InputStream stream = this.getClass()

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值