Read from a file

Put your problem here if it does not fit any of the other categories.

Read from a file

Postby himym » Mon Nov 09, 2009 12:52 pm

Hello.
I want to read from a .txt file on my device (G1). I have (and created) that file on my PC, I then pushed it onto my G1 via "adb -s <devicenumber> push <localpath> /sdcard/download/test.txt", this seemed to work since it showed me the connection-velocity and everything (Do I need to put that file somewhere else?). Now I want to access that file:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. FileInputStream f = null;
  3.  
  4.                 try {
  5.  
  6.                         f = ctx.openFileInput("/sdcard/download/test.txt");
  7.  
  8.                 } catch (FileNotFoundException e) {
  9.  
  10.                         // TODO Auto-generated catch block
  11.  
  12.                         e.printStackTrace();
  13.  
  14.                 }
  15.  
  16.                 StringBuffer buffer = new StringBuffer();
  17.  
  18.                 DataInputStream dataIO = new DataInputStream(f);
  19.  
  20.                 String line = null;
  21.  
  22.                 try {
  23.  
  24.                         while((line = dataIO.readLine()) != null){
  25.  
  26.                                 buffer.append(line+"\n");
  27.  
  28.                         }
  29.  
  30.                 } catch (IOException e) {
  31.  
  32.                         // TODO Auto-generated catch block
  33.  
  34.                        
  35.  
  36.                         e.printStackTrace();
  37.  
  38.                 }
  39.  
  40.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


But I somehow get an error, when I want to access the DataInputStream.
Can anybody help me please?????

Thank you so much!!!!
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Top

Postby wiederke » Mon Nov 09, 2009 1:14 pm

Hi,

if you use the openFile methods of the context. The file must be located in the files directory of the application. Read this: http://developer.android.com/guide/topi ... html#files

To load files from the sdcard just use standard java classes:
FileInputStream fis = new FileInputStream("/sdcard/download/test.txt"))


Bye,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby himym » Mon Nov 09, 2009 1:34 pm

hey thorsten,
thanks a lot for your advice. The thing with reading from the sd-card is working. The other possibility not though, but for the beginning that should be fine.
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 11 guests