【问题标题】:Eclipse get linked resource path for file loadingEclipse获取文件加载的链接资源路径
【发布时间】:2019-06-24 23:49:34
【问题描述】:

在我的 Eclipse 项目中,我有一个从一个驱动器文件夹链接的“src”文件夹。 我在链接文件夹中有一些其他文本文件,我想用FileReader 加载。

我将如何获得这个位置,最佳方式是不知道该文件夹是链接的还是实际在项目文件夹中。我试过使用

MyClass.class.getResource("");

但它会返回“bin”文件夹的路径。我可能没有正确使用它。我要获取的文件是“src/de/lauch/engine/shaders/primitiveTestShader/vertexShader.vsh”

提前致谢!

【问题讨论】:

    标签: java eclipse resources filereader getresource


    【解决方案1】:

    您可以创建像 'src\main\resources' 这样的资源文件夹,然后将文件放在之后,您就可以运行相同的代码。希望它会起作用。

    【讨论】:

    • 我想要获取链接文件夹位置的原因是它会自动与我的其他设备同步。因此,在我的项目中创建文件夹不是一种选择。谢谢你的回答。
    【解决方案2】:

    我现在解决了我的特定问题,但我仍然愿意接受更好的解决方案:)

    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”等项目本地路径转换为这些链接的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多