我制作了一个应用程序并将其作为新文件夹放入 webapps 中,例如 appcustom。
我想阅读一些文本文件。
我如何一般地读取而不是硬编码路径?
目前我正在使用这样的东西:
FileReader fr = new FileReader("webapps/appcustom/<<textfilename>>"); // read a file
如何使其通用?因为明天我可能想将应用程序文件夹名称更改为“custApp”或其他名称。
最佳答案
您可以在 Red5 中使用一些系统属性,如果我尝试从通用应用程序类读取资源,我将使用此 block :
FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), appName, fileName));
需要提供应用程序名称(例如:oflaDemo)和您的文件名。
从 ApplicationAdapter 您可以访问“可用”应用程序名称:
FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), getScope().getName(), fileName));
从 Servlet,您可以访问完整路径:
FileReader fr = new FileReader(String.format("%s/%s", getServletContext().getRealPath("/"), fileName));
关于java - 如何在Red5中获取应用程序路径(绝对和相对),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4549108/