在我的Eclipse项目中,我有一个“src”文件夹,它是从一个驱动器文件夹链接的。我在链接文件夹中有一些其他文本文件,我想用FileReader加载。
我怎么会得到这个……
你可以像'src \ main \ resources'那样创建资源文件夹,然后放入文件,你可以运行相同的代码。希望它会奏效。
我现在解决了我的特殊问题,但我仍然愿意接受更好的解决方案:)
public class LinkedResourceLocator { private static Dictionary<String,String> locations; public static String getPath(String path) { if(locations==null) { File projectLocal = new File(LinkedResourceLocator.class.getClassLoader().getResource("").getPath().replaceAll("%20", " ")).getParentFile(); File dotProject = new File(projectLocal.getAbsolutePath()+"\\.project"); locations = new Hashtable<String,String>(); File[] files = projectLocal.listFiles(new FileFilter(){ @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); for (int i = 0; i < files.length; i++) { locations.put(files[i].getName(), files[i].getAbsolutePath()); } try { BufferedReader br = new BufferedReader(new FileReader(dotProject)); StringBuilder fileContentBuilder = new StringBuilder(); String line; while((line = br.readLine()) != null) { fileContentBuilder.append(line.trim()); } String fileContents = fileContentBuilder.toString(); Pattern p = Pattern.compile("<link><name>(\\w*)</name><type>\\d*</type><location>([\\w/:]*)</location></link>"); Matcher m = p.matcher(fileContents); while(m.find()) { locations.put(m.group(1),m.group(2)); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.println("Can't locate .project file"); } catch (IOException e) { e.printStackTrace(); System.err.println("Can't read .project file"); } } String locator = path.contains("/")?path.substring(0, path.indexOf("/")):path; String restPath = path.substring(locator.length()); return locations.get(locator)+restPath; } }
此类从eclipse .project文件获取链接的资源位置,然后将项目本地路径(如“src / de / lauch / engine / shaders / primitiveTestShader / vertexShader.vsh”)转换为这些链接的位置。