read a .txt file from sd card in a real device

Quickly share your Android Code Snippets here...

read a .txt file from sd card in a real device

Postby noya » Fri Jun 12, 2009 4:10 pm

I use this code to access a file on the sdcard in the real device:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.BufferedReader;
  2.  
  3. import java.io.File;
  4.  
  5. import java.io.FileInputStream;
  6.  
  7. import java.io.FileNotFoundException;
  8.  
  9. import java.io.IOException;
  10.  
  11. import java.io.InputStreamReader;
  12.  
  13. import android.os.Environment;
  14.  
  15. import android.util.Log;
  16.  
  17.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


i read a file named "filename.txt" put in the root of the sdcard (I pushed the file using Eclipse in the "sdcard" directory on the phone)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try{
  2.  
  3.    File f = new File(Environment.getExternalStorageDirectory()+"/filename.txt");
  4.  
  5.    fileIS = new FileInputStream(f);
  6.  
  7.    BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
  8.  
  9.    String readString = new String();
  10.  
  11.    //just reading each line and pass it on the debugger
  12.  
  13.    while((readString = buf.readLine())!= null){
  14.  
  15.       Log.d("line: ", readString);
  16.  
  17.    }
  18.  
  19. } catch (FileNotFoundException e) {
  20.  
  21.    e.printStackTrace();
  22.  
  23. } catch (IOException e){
  24.  
  25.    e.printStackTrace();
  26.  
  27. }
  28.  
  29.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Top

Postby Angie » Sat Oct 17, 2009 11:31 pm

Thanks a lot, was looking for this! ;)
Angie
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Oct 11, 2009 2:56 pm

Postby spring » Sat Jan 09, 2010 12:03 pm

good!
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Postby GameQ » Tue Feb 09, 2010 5:40 pm

You'd be surprised at how long I was searching for just this kind of information. Thanks!
Rule #1: Nothing ever works on the first try
GameQ
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Dec 11, 2009 6:11 am

Re: read a .txt file from sd card in a real device

Postby rayhaque » Tue Nov 02, 2010 9:59 pm

I have been looking for a code snippet like this also! I believe there is a typo though ...

Shouldn't line 5 in the second block look like this?

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
  1. FileInputStream fileIS = new FileInputStream(f);
Parsed in 0.005 seconds, using GeSHi 1.0.8.4
rayhaque
Freshman
Freshman
 
Posts: 3
Joined: Wed Sep 22, 2010 4:32 pm

Re: read a .txt file from sd card in a real device

Postby raultm » Tue Jan 04, 2011 2:51 pm

Thanks. It is helpful.
raultm
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 04, 2011 2:39 pm
Location: Almendralejo

Top

Re: read a .txt file from sd card in a real device

Postby eugenevdm » Sun Mar 27, 2011 11:04 am

rayhaque wrote:...Shouldn't line 5 in the second block look like this?...

Yes it should. The author did not add that little bit of essential code.
eugenevdm
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 27, 2011 8:57 pm

Re: read a .txt file from sd card in a real device

Postby Mike.Adams » Wed Apr 06, 2011 5:01 pm

thanks for that piece of code
Hi I'm Mike. Checkout my source code, projects and work here and let me know what you think.

Feel free to also upload and submit your code/projects there for all to see and find!
Mike.Adams
Developer
Developer
 
Posts: 26
Joined: Mon Mar 07, 2011 9:19 pm
Location: New York

Re: read a .txt file from sd card in a real device

Postby igor » Mon Nov 05, 2012 10:54 am

thanks, it's Working
but its not working when the to the computer or outher device.
How can I make it work when i additing the file from the usb?
igor
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Nov 05, 2012 9:25 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests