无法读取刚刚写入的文件

时间:2013-04-14 09:56:58

标签: java android file-io

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

 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.为什么刚写入的文件没有被读取?

1 个答案:

答案 0 :(得分:1)

代码的一些内容:

  1. @Oren是正确的,您应该使用Log.e(TAG, "message", e)而不是eclipse中自动创建的东西!
  2. 你应该简单地将3 try / catch合并为一个。不需要做3次......
  3. 您也应该使用Log.e()如上所述FileNotFoundException,所以请查看堆栈跟踪以查看真实原因(目前涵盖解决问题的提示)
  4. 如果您至少完成了3号,那么您会看到找不到您尝试阅读的文件。这就是为什么你的日志没有显示if语句的Can open userCredentials for reading from.输出。

    原因很简单:使用openFileOutput(userCredFile, MODE_PRIVATE);创建文件。如果您阅读documentation of openFileOutput,您会偶然发现:

      

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

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

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

    主要问题,以及问题的答案:为什么不使用相应的openFileInput(userCredFile)方法来阅读文件?