-2
    private void monthlyInput() throws IOException {
        File inFile = new File(Environment.getExternalStorageDirectory       ().getAbsolutePath(), "S050213.txt");

        String data = inFile.getAbsolutePath();
        TextView testData = (TextView) findViewById(R.id.textView1);
        testData.setText(data);

        FileInputStream fInStream = new FileInputStream(inFile);


        BufferedReader myReader = new BufferedReader(new InputStreamReader(fInStream));

        String data2 = myReader.readLine();

        TextView testData2 = (TextView) findViewById(R.id.textView2);
        testData2.setText(data2);

        myReader.close();

    }

I know this is finding the file because data is outputting the correct file path, but data2 seems to be empty. My best guess is that it isn't finding the file at

FileInputStream fInStream = new FileInputStream(inFile);

I'm wondering where I am going wrong, or if I am even close to where I should be.

8
  • 2
    Is it throwing an exception? If not, your file probably exists, but is empty
    – nullptr
    May 5, 2013 at 5:38
  • before reading check whether file exists or not.. if(inFile.exists()) then only do stuff
    – Pragnani
    May 5, 2013 at 5:40
  • If there is an exception check permission May 5, 2013 at 5:40
  • I added in the if statement and it goes to the else, so I assume this means there is an exception. I have <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in the Manifest.xml, do I need to do something else for permission?
    – CFIT
    May 5, 2013 at 5:55
  • 1
    @Pragnani Why? The code will throw a FileNotFoundException. Checking exists() as well adds nothing.
    – user207421
    May 5, 2013 at 22:04

1 Answer 1

0

Code looks fine. Seems like issue reading the file. Are you sure it's being saved properly. Try placing a file with some random text with different unique name - manually in your external storage directory and try reading it with same code.

3
  • Tried putting a different text file on the device, and that read fine. Then I removed the file I need and put it back on the device and it is reading fine now. Still not sure what the problem was, but thanks for pointing me to the file rather than the code.
    – CFIT
    May 5, 2013 at 16:16
  • Sorry to be a bother, but I have one more question. Can I specify to read the file from downloads directory which is in the same directory as the file is currently?
    – CFIT
    May 5, 2013 at 18:02
  • You can try Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    – Anirudh
    May 6, 2013 at 6:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.