我如何在jar中使用FileInputStream getclass()。getresource()来加载txt文件

时间:2013-10-11 16:17:24

标签: java fileinputstream

如何在jar中加载文本文件? 无法使用getClass().getResource("/DataBase.txt");

public static void readFromFile() throws FileNotFoundException, IOException, ClassNotFoundException{

        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Authorisation.userMap = (HashMap<String, User>) ois.readObject();
        ois.close();
    }

1 个答案:

答案 0 :(得分:0)

您没有阅读实际文件,请使用:

InputStream in = getClass().getResourceAsStream("/DataBase.txt");
ObjectInputStream ios = new ObjectInputStream(in);