[英]How to get the path of the sketch in Processing ? (dataPath not working)
使用Processing 3-0-1(尚未更新),我遇到了dataPath()函数的问题。
我的代码:
final String path = dataPath("");
//...
void setup(){
//...
println(path);
//...
}
这将显示在控制台中:
/ media / ubuntu / Expansion Drive / Programmation / Java / Processing / Ubuntu / processing-3.0.1 / null / data
但它应该显示(根据文档和我在其他项目中的尝试):
/ media / ubuntu / Expansion Drive / Programmation / Java / Processing / Projets / Time_Fighter / data
所以它似乎正在返回一个路径Processing而不是我的项目的路径? 另外,为什么会出现“空”?
问题是,你知道一种方法可以使我的草图的路径?
PS。 我的一个朋友建议使用File类,我得到了这个结果:
File file = new File("data");
//...
void setup(){
//...
file.getAbsolutePath();
//...
}
返回的是:
/ media / ubuntu / Expansion Drive / Programmation / Java / Processing / Ubuntu / processing-3.0.1 / data
PPS。 我正在使用Ubuntu(Mate)......
您不应该使用dataPath()
函数。 您应该使用sketchPath()
函数。
/**
* <b>This function almost certainly does not do the thing you want it to.</b>
* The data path is handled differently on each platform, and should not be
* considered a location to write files. It should also not be assumed that
* this location can be read from or listed. This function is used internally
* as a possible location for reading files. It's still "public" as a
* holdover from earlier code.
* <p>
* Libraries should use createInput() to get an InputStream or createOutput()
* to get an OutputStream. sketchPath() can be used to get a location
* relative to the sketch. Again, <b>do not</b> use this to get relative
* locations of files. You'll be disappointed when your app runs on different
* platforms.
*/
public String dataPath(String where) {
return dataFile(where).getAbsolutePath();
}
这里是sketchPath()
:
/**
* Prepend the sketch folder path to the filename (or path) that is
* passed in. External libraries should use this function to save to
* the sketch folder.
* <p/>
* Note that when running as an applet inside a web browser,
* the sketchPath will be set to null, because security restrictions
* prevent applets from accessing that information.
* <p/>
* This will also cause an error if the sketch is not inited properly,
* meaning that init() was never called on the PApplet when hosted
* my some other main() or by other code. For proper use of init(),
* see the examples in the main description text for PApplet.
*/
public String sketchPath(String where) {
if (sketchPath() == null) {
return where;
}
// isAbsolute() could throw an access exception, but so will writing
// to the local disk using the sketch path, so this is safe here.
// for 0120, added a try/catch anyways.
try {
if (new File(where).isAbsolute()) return where;
} catch (Exception e) { }
return sketchPath() + File.separator + where;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.