本文介绍了从另一项活动读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读,我曾在其他活动使用OutputStreamWriter写入一个文本文件。
这是我readFromFile方法Sale.java:

I want to read a text file that i had write in another activity using OutputStreamWriter.this is my readFromFile method in Sale.java:

private int readFromFile(String request) {

        int res = 0;
        try {
            //InputStream inputStream = openFileInput("dalassnums.txt");
            File file=new File("dalassnums.txt");
            InputStream inputStream = new FileInputStream(file);

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    String s=bufferedReader.readLine();
                    if(receiveString==request)  {
                        res=Integer.valueOf(s);
                        break;
                    }
                }

                inputStream.close();

            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
            res=0;

        } catch (IOException e) {
            //Log.e("login activity", "Can not read file: " + e.toString());

        }

        return res;



    }

这是将writeToFile方法MainActivity.java:

And this is writeToFile method in MainActivity.java:

private void writeToFile2(String numchar) {
    try {
        //File file=new File("dalassnums.txt");
        //OutputStream outputStream=new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter;
        if(numchar=="1") outputStreamWriter = new OutputStreamWriter(openFileOutput("dalassnums.txt", Context.MODE_PRIVATE));
        else  outputStreamWriter = new OutputStreamWriter(openFileOutput("dalassnums.txt", Context.MODE_APPEND));
        for(int k=0; k<imageNums.size();k+=2){
        outputStreamWriter.append(imageNums.get(k));
        outputStreamWriter.append("\n");
        outputStreamWriter.append(imageNums.get(k+1));
        outputStreamWriter.append("\n");
        }
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

当执行readFromFile,则返回0,则表示未找到文件;
我读到过上下文,但是我不知道通过什么情况下;而且不知道是否有比传递上下文的任何其他方式。
任何帮助将是AP preciated。

When performing readFromFile, it returns 0 that means file not found;I read about passing context but i don't know what context to pass; And wondering if there is any other way than passing context.Any help would be appreciated.

推荐答案

使用: openFileInput readFromFile ,看这里有个例子:

Use : openFileInput in readFromFile, look here for example:

openFileInput()和/或openFileOutput()I / O流默默未能

的另一问题是,这是无效的:

Another problem is that this is invalid:

if(numchar=="1")

您应该

if(numchar.equals("1"))

否则你比较基准值,而不是字符串的内容

otherwise you compare reference values instead content of string

这篇关于从另一项活动读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:21