Question

I am trying to add the contents of a text file to a content box in my app. When I give the name of the .txt file, the contents of the txt file should get placed in my content box. This txt file is placed in the sdcard directory.

Here is the code I have written

public void sdd(View v){

    TextView t = (EditText)findViewById(R.id.txtTitle);
    EditText content = (EditText)findViewById(R.id.txtContent);
    File dir = Environment.getExternalStorageDirectory();
    String tt=".txt";
   // File file = new File(dir,"text.txt");
    File file = new File(dir,t.getText().toString()+ tt);
    if(file.exists())   // check if file exist
    {
          //Read text from file
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }
        //Set the text
      //  et.setText(text);
        content.setText(text);
    }
    else
    {
        content.setText("Sorry file doesn't exist!!");
    }
 }  

Now, when I am trying to give the file name, I am always getting Sorry file doesn't exist, even though it does! Can anyone tell me what's wrong? Or how to improve this code

Was it helpful?

Solution

Better to check File like this:

File dir = Environment.getExternalStorageDirectory().getAbsolutePath();
String filename = t.getText().toString()+ tt;
File file = new File(dir,filename.trim());

if(file.exists() && (file.length()!=0))   // check if file exist
{

//do your job

}else{

 content.setText("Sorry file doesn't exist!!");

}

And add Read and Write permission in manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

OTHER TIPS

I'm just starting out with Android dev too!

First I'd create a new variable to store this: t.getText().toString()+ tt

Then I'd trying debugging your code. Set a breakpoint at the line where you test for file existence, that's clearly returning false.

Check the content of that variable to be sure it's got the filename you're expecting

Hope that helps!

Take permission both read and write at this time u only need read permission

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

May be some blank space have a prob then try wd this

TextView t = (EditText)findViewById(R.id.txtTitle);
    EditText content = (EditText)findViewById(R.id.txtContent);
    File dir = Environment.getExternalStorageDirectory();
    String tt=".txt";

String filename = getText().toString()+ tt; 


    File file = new File(dir,filename.trim());

remaining code is same

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top