将文本文件加载到android项目时出现FileNotFoundException

时间:2015-08-17 06:02:25

标签: java android hashmap bufferedreader filenotfoundexception

我一直在为一个类的Android竞价页面应用程序工作,我似乎无法找出当我的路径正确(相对路径)时我的程序抛出FileNotFoundException的原因。我使用这个类来读取包含Item对象的字符串值的文件,并创建要放入临时HashMap的项目并将其返回。在此先感谢!!

    public class FileLoader 
    {
        private File path = new File("items");
        private static File textFile;

        //fileName is called with "AllItems.txt"
        public FileLoader(String fileName)
        {
            textFile = new File(path, fileName);
        }

        public Map<Integer, Item> getItems()
        {
            //Item is a custom Object
            Map<Integer, Item> items = new HashMap<Integer, Item>();
            int i = 1;
            try (BufferedReader bf = new BufferedReader(new FileReader(textFile));)
            {
                String itemToParse;
                while((itemToParse = bf.readLine()) != null)
            {
                //ItemParser is a custom static class to create an item from one string
                items.put(i, ItemParser.ParseItem(itemToParse));
                i++;
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return items;
    }
}

0 个答案:

没有答案
相关问题