简体   繁体   中英

ASCII file in Eclipse plugin project not visible after export

Am am working on an Eclipse plugin project. Inside that project I have a folder resources , which contains a simple ASCII file, which contains lines with String s. These String s are used in a drop down menu, by the ui. If I run a new launchtime Eclipse everything works like it should and the file content is successfully used by the drop down menu but if I export the application and run it, the drop down menu is empty, which means, that the file can't be read.

Inside the plugin.xml (Build tab) I've marked the resource folder (which contains the ASCII file) in the binary build.

This is how I extract the file line by line:

public class ParameterExtractor {

    private final static String FILE_PATH = "/resources/parameters";

    public ArrayList<String> extractParameters() throws IOException {
        ArrayList<String> params = new ArrayList<String>();
        URL url = FileLocator.resolve(getClass().getResource(FILE_PATH));
        URI uri = null;
        try {
            uri = url.toURI();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (uri != null) {
            try {
                File file = new File(uri);
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    params.add(line);
                }
                fileReader.close();
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
        return params;
    }
}

And this is how I use the returned ArrayList :

ArrayList<String> params = new ArrayList<String>();
try {
    params = new ParameterExtractor().extractParameters();
} catch (IOException e1) {
    e1.printStackTrace();
}
final Combo combo = new Combo(container, SWT.BORDER);
combo.setItems(params.toArray(new String[params.size()]));

What could cause the problem and how can I solve it?

I would be grateful for any help!

FileLocator.resolve does not guarantee to return you a URL than can be used to read a file. Instead use:

Bundle bundle = ... your plugin bundle

URL url = FileLocator.find(bundle, new Path(FILE_PATH), null);

url = FileLocator.toFileURL(url);

The URL returned by toFileURL is suitable for reading using File and FileReader .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM