如何在 Processing 中获取草图的路径? (数据路径不工作)
How to get the path of the sketch in Processing ? (dataPath not working)
使用处理 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 的路径而不是我的项目的路径?另外,为什么会有 'null' ?
问题是,你知道有什么方法可以得到我的草图路径吗?
PS.
我的一个朋友建议改用文件 class,我得到了这个结果:
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()
函数。
来自the Processing source for dataPath()
:
/**
* <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;
}
sketchPath()
只在setup()
后有效,不能
String path = sketchPath();
setup() {
}
相反,您需要这样做:
String path;
setup() {
path = sketchPath();
}
(处理 3.5.4)
使用处理 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 的路径而不是我的项目的路径?另外,为什么会有 'null' ?
问题是,你知道有什么方法可以得到我的草图路径吗?
PS. 我的一个朋友建议改用文件 class,我得到了这个结果:
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()
函数。
来自the Processing source for dataPath()
:
/**
* <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;
}
sketchPath()
只在setup()
后有效,不能
String path = sketchPath();
setup() {
}
相反,您需要这样做:
String path;
setup() {
path = sketchPath();
}
(处理 3.5.4)