java - Get a file from a given classpath -
public string getquery(string namefile, package pathfile) { // how on inputstrem namefile , pathfile } i not able make through classloader
string path = getclass().getpackage().getname().replace('.', file.pathseparatorchar); string file = path + "file.txt"; inputstream in = this.getclass().getclassloader().getresourceasstream(file); return in = null
the pathseparatorchar : on unix , ; on windows. has nothing char used load resources classloader, /, on platforms.
moreover, forgot separator between path , file name. should be
string path = getclass().getpackage().getname().replace('.', '/'); string file = path + "/file.txt"; inputstream in = this.getclass().getclassloader().getresourceasstream(file); or, simpler, since class has method can load resources same package class directly:
inputstream in = this.getclass().getresourceasstream("file.txt");
Comments
Post a Comment