【问题标题】:Android - Read a FileAndroid - 读取文件
【发布时间】:2013-08-05 23:50:41
【问题描述】:

我正在尝试从我的/assests 文件夹中的exercises.txt 文件中读取练习列表,并且我找到了很多示例,但我不断收到错误“无法解析上下文”,如果我设法解决这个问题,然后我得到“默认构造函数无法处理隐式超级构造函数抛出的异常类型 IOException。必须定义一个显式构造函数”

这是我的代码:

class ChooseExercises extends ListActivity{

    String[] exercises;

    AssetManager am = context.getAssets();  //Error 1
    InputStream inputStream = am.open("test.txt"); //Error 2
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_exercises);
    }

}

感谢大家的帮助。

【问题讨论】:

    标签: android file-io constructor implicit super


    【解决方案1】:

    由于没有任何名称为 context,因此您无法从数据成员初始化程序中引用它。

    因此,首先将您的AssetManager 和后续数据成员作为局部变量移动到onCreate(),然后将context.getAssets() 替换为getAssets(),这样您的状态会更好。

    class ChooseExercises extends ListActivity{
        String[] exercises;
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.choose_exercises);
    
            AssetManager am = context.getAssets();  //Error 1
            InputStream inputStream = am.open("test.txt"); //Error 2
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
            // TODO: actually use this stuff
        }
    }
    

    稍后,当您对 Java 和 Android 越来越熟悉时,将此磁盘 I/O 移至后台线程。

    【讨论】:

    • 您推荐什么方法从 Web 服务读取数据?我给你买了书,你通常会逐行阅读其中的回复。我不知道你的书有没有论坛,所以我在stackoverflowgoo.gl/9MdzpL中问了这个问题
    【解决方案2】:

    我想你忘了使用 try-catch 结构。

    写作:

    try{
                FileOutputStream fos = getBaseContext().openFileOutput("fileName", Context.MODE_PRIVATE);
                ObjectOutputStream os = new ObjectOutputStream(fos);
                os.writeObject("content");
                os.close();
            }catch(Exception ex){
                //do stuff if something goes wrong.
            }finally{
                //do stuff in the end.
            }
    

    阅读:

    try{
            FileInputStream fis = getBaseContext().openFileInput("filename");
            ObjectInputStream is = new ObjectInputStream(fis);
            Object s = (Object) is.readObject(); // Object must be String if you want to read a string.
            is.close();
        }catch(Exception ex){
            //do stuff if something goes wrong.
        }finally{
            //do stuff in the end.
        }
    

    【讨论】:

    • am.open("test.txt");需要try-catch
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2011-05-21
    • 2015-05-19
    • 1970-01-01
    • 2014-10-01
    • 2016-05-16
    相关资源
    最近更新 更多