Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read text file from SD card

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 326
    Comment on it

    Below is the code to read a text file from SD card.

    First, we have to define path of the text file to a File.

    Second step is to use BufferedReader class to read line by line.

    1. File sdcard = Environment.getExternalStorageDirectory();
    2.  
    3.  
    4. File readFile = new File(sdcard,"reading.txt");
    5.  
    6. //Read text from file
    7. StringBuilder stringBuilder = new StringBuilder();
    8.  
    9. try {
    10. BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));
    11. String str;
    12.  
    13. while ((str = bufferedReader.readLine()) != null) {
    14. stringBuilder.append(line);
    15. stringBuilder.append('\n');
    16. }
    17. bufferedReader.close();
    18. }
    19. catch (IOException e) {
    20. System.out.println("Error : "+e.getMessage());
    21. }
    22.  
    23. TextView messageFile = (TextView)findViewById(R.id.message_text);
    24.  
    25. messageFile.setText(text.toString());

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: