在主要的文本文件的Java相对路径? [英] Java- relative path of text file in main?

查看:143
本文介绍了在主要的文本文件的Java相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何指定一个相对路径名称到存储在我的src文件夹的java项目的文本文件。我想存储一个字符串与此文本文件的路径。例如,如果我有我的Java项目的src文件夹中的example.txt我怎么去寻找它的相对路径?我也在做我的主,所以我无法使用.getResource()。我将如何做到这一点?感谢。
$ b

我的文件路径跟eclipse中的属性一样
/MyProject/src/data.txt

我试过了:

String path =/MyProject/src/data.txt; $ b $如果你使用的是eclipse,把你的文本文件放在根目录下项目文件夹,位于 / src / bin 文件夹之外。它应该现在可以直接通过相对路径访问。



如果要访问src文件夹中的文件,您必须添加 / src / 前面的文件路径/名称


I was wondering how to specifiy a relative path name to a text file that is stored in my src folder of a java project. I would like to store a string with the path of this text file. For example if I had example.txt located in the src folder of my java project how would I go about finding its relative path? I am also doing this in my main so I'm having trouble using .getResource(). How would I do this? Thanks.

My files path is as followed from the properties in eclipse /MyProject/src/data.txt

I've tried:

String path = "/MyProject/src/data.txt";

But that doesn't work?

解决方案

If you are using eclipse, place your text file in the root of the project folder, outside the /src and /bin folders. It should be now accessible via a relative path directly.

If you want to access a file in src folder, you have to append the /src/ prefix before the file path/name

这篇关于在主要的文本文件的Java相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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