get File from resource - Java Internationalization

Java examples for Internationalization:Resource File

Description

get File from resource

Demo Code


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URISyntaxException;
import java.net.URL;

public class Main{
    public static void main(String[] argv) throws Exception{
        String file = "java2s.com";
        System.out.println(getFile(file));
    }/*w  w  w.j  av a 2s  .c  o  m*/
    private static File getFile(String file) {
        return new File(new File(getBaseFolder(), "buffer/"), file);
    }
    private static File getBaseFolder() {
        return getResourceAsFile("/");
    }
    private static File getResourceAsFile(String file) {
        URL url = Main.class
                .getResource(file);
        try {
            return new File(url.toURI());
        } catch (URISyntaxException e) {
            return new File(url.getPath());
        }
    }
}

Related Tutorials