0

我正在尝试从写入了一些用户凭据的文件中读取。我想将文件写入内部存储位置。编码:

 private void writeSendDetails(String name, String number, String emailID) {

        //This function writes details to userCredentials.txt and also sends it to server.
        String text = "Name: " + userName + "\n" + "Number: " + userNumber + "\n" + "Email ID:" + userEmailID;
        FileOutputStream fos = null;
        try {
            fos = openFileOutput(userCredFile, MODE_PRIVATE);
            Log.v(this.toString(), fos.toString());
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        if(fos != null) {
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            try {
                osw.write(text);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.v(this.toString(), "IOException caught in osw.write");
                e.printStackTrace();
            }
            try {
                osw.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                osw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Log.v(this.toString(), "Written everything to userCredentials.txt");
        readUserCredentials();
        //code to send to server should begin here.

    }

private void readUserCredentials() {
        //function to read name, number and email ID from userCredentials.txt
        /* Steps:
         * 1. Check if file exists.
         * 2. If it does, read all relevant credentials.
         */

        File f = new File(userCredFile);
        if(f.canRead()) {
            Log.v(this.toString(), "Can open userCredentials for reading from.");
        }

        try {
            FileReader fis = new FileReader(f);
            Log.v(this.toString(), "Wrapping a buffered reader around file reader.");
            BufferedReader bufRead = new BufferedReader(fis, 100);
            String line;
            try {
                while((line = bufRead.readLine()) != null) {
                    Log.v(this.toString(), "Line read = " + line);
                    line = bufRead.readLine();
                    if(line.indexOf("Name: ") != -1) {
                        Log.v(this.toString(), "Found name in the string.");
                        userName = line.substring(6);
                    } else if(line.indexOf("Number: ") != -1) {
                        Log.v(this.toString(), "Found number in the string.");
                        userNumber = line.substring(8);
                    } else if(line.indexOf("Email ID: ") != -1) {
                        Log.v(this.toString(), "Found email in the string.");
                        userEmailID = line.substring(10);
                    }
                }
                Log.v(this.toString(), "User credentials = " + userName + "   " + userNumber + "    " + userEmailID);
            } catch (IOException e) {
                Log.v(this.toString(), "IOException caught.");
            }
        } catch (FileNotFoundException e1) {
            Log.v(this.toString(), "File not found for reading.");
        }
    }

LogCat输出显示 :

04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay@44c0c660(675): Written everything to userCredentials.txt
04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay@44c0c660(675): File not found for reading.
04-14 15:20:43.889: V/com.sriram.htmldisplay.fireflymenu@44c401e0(675): File not found for reading.

我的问题:
1. 我需要将文件写入内部存储。我做得对吗?
2. 为什么刚刚写入的文件没有被读取?

4

1 回答 1

1

您的代码的一些内容:

  1. @Oren 是正确的,您应该使用Log.e(TAG, "message", e)而不是 eclipse 中自动创建的东西!
  2. 您应该简单地将 3 个 try/catch 合并为一个。没必要做3次...
  3. 您也应该Log.e()如上所述使用FileNotFoundException,因此请查看堆栈跟踪以检查真正的原因(目前涵盖了解决您问题的提示)

如果您至少完成了第 3 步,您会发现您尝试读取的文件无法找到。这就是为什么您的日志不显示Can open userCredentials for reading from.if 语句的输出的原因。

原因很简单:您使用openFileOutput(userCredFile, MODE_PRIVATE);. 如果您阅读openFileOutput 的文档,您会偶然发现:

要打开的文件的名称;不能包含路径分隔符。

这意味着userCredFile只能是test.txt. 此外,此方法会在无法从“外部”轻松访问的目录中创建文件。

当您现在尝试通过FileReader(userCredFile)它读取文件时,应该很明显,android 将尝试在根目录中打开它:/text.txt当然,它会失败。没有非 root 应用程序可以在根目录中写入/读取。

主要问题,也是您问题的答案:您为什么不使用相应的openFileInput(userCredFile)方法读取文件?

于 2013-04-14T10:32:50.130 回答