简体   繁体   English

如何在处理中使用相对路径而不是绝对路径?

[英]How to use a relative path instead of an absolute path in processing?

I am fairly new to processing (and programming in general). 我对处理(和一般而言的编程)还很陌生。

In a project that I'm doing I need to access the following path: 在我正在做的项目中,我需要访问以下路径:

f = new File("C:/Users/Matthew/ColourFeature/data/image1.jpg");

This, being an absolute path, works without any problem. 这是绝对路径,可以毫无问题地工作。

However since this program will be run on different machines I am trying to figure out how to make use of an relative path in processing to access the path above. 但是,由于该程序将在不同的机器上运行,因此我试图找出如何在处理中使用相对路径来访问上述路径。 By the way, for the purpose of my project I cannot make use of processing's loadImage(). 顺便说一句,出于我的项目目的,我无法利用处理的loadImage()。

Any help will be greatly appreciated 任何帮助将不胜感激

You can get the user's home folder by: 您可以通过以下方式获取用户的主文件夹:

String userHome = System.getProperty("user.home");

Which in your case would return "C:/Users/Matthew" . 您的情况将返回"C:/Users/Matthew"

If you agree to use a fixed path inside of this, you can get your image file like: 如果您同意在其中使用固定路径,则可以得到如下图像文件:

f = new File(userHome, "/ColourFeature/data/image1.jpg");

This constructor of File takes 2 arguments: a parent folder and a relative child folder and/or file. File构造方法带有2个参数: 文件夹和相对子文件夹和/或文件。

Now this f file will point to a proper file in all machines if the currently logged-in user has a file named "image1.jpg" in the "/ColourFeature/data" folder inside his/her home folder. 现在,如果当前登录的用户在其主文件夹内的"/ColourFeature/data"文件夹中有一个名为"image1.jpg"文件,则此f文件将指向所有计算机中的正确文件。

If the file is within your code, you could use the getResource method of the classLoader. 如果文件在代码内,则可以使用classLoader的getResource方法。

eg 例如

File newFile = new File(YourClass.class
                   .getResource("relative/path/from/YourClass.java")
                   .toString());

If you need to access files in a parent directory of the class you could use the method described by @palacsint here 如果您需要访问,你可以使用由@palacsint描述的方法的类的父目录的文件在这里

The normalize() methods (there are four of them) in the FilenameUtils class could help you. FilenameUtils类中的normalize()方法(有四个)可以为您提供帮助。 It's in the Apache Commons IO library. 它在Apache Commons IO库中。

 final String name = "/a/b/../"; final String normalizedName = FilenameUtils.normalize(name, true); // "/a/" getClass().getResource(normalizedName); 

I believe that Processing makes this quite easy if you simply use the "data" path that is used by default with each sketch - simply a subfolder called "data" in your sketch folder. 我相信,如果您仅使用默认情况下每个草图使用的“数据”路径-只需在草图文件夹中使用一个名为“数据”的子文件夹,“处理”将使此操作非常容易。

Put all your images (or any other files, for that matter) in the data folder, and you will typically not need to specify an absolute path in most cases (eg with loadImage() ). 将所有图像(或其他任何文件)放在数据文件夹中,在大多数情况下(例如,使用loadImage() ),通常不需要指定绝对路径。

Of course, since you said you can't use loadImage() , you can always access the path to the data folder using the provided method dataPath("") . 当然,由于您说不能使用loadImage() ,因此始终可以使用提供的方法dataPath("")访问数据文件夹的路径。 For example, when I do 例如,当我做

println(dataPath(""));

from my example sketch, I get the following output: /Users/stephenbuchanan/Documents/Processing/testsketch/data 从我的示例草图中,我得到以下输出: /Users/stephenbuchanan/Documents/Processing/testsketch/data

Thanks for the help. 谢谢您的帮助。 I turned out to be quite simple. 我原来很简单。 The solution is the following: 解决方案如下:

f = new File(dataPath(""), "/image1.jpg");

However it is worth mentioning that using dataPath("") as a global variable give a different path (I assume it is the sketchbook path) then using dataPath("") within the setup method. 不过值得一提的是,使用dataPath("")作为一个全局变量给出了不同的路径,然后使用(我假设它是写生路径) dataPath("")的安装方法中。

I sorry if I didn't use the correct technical words but i'm fairly new to programming. 很抱歉,如果我没有使用正确的技术词汇,但是对编程我还是很陌生。

Thanks again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM