How to write to the File ?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

How to write to the File ?

Postby javame_android » Sat Apr 04, 2009 11:29 am

Hi All,

I would like to know how to write to file. I have tried using

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. OutPutStream out = new FileOutPutStream(fileName);
  3.  
  4. out.write(byte [], 0, length);
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This way always returns Parent directory or File is not writable.

Help me out! Does anyone knows what this error is about and how it can be removed.
Thanks & Regards
Sunil
javame_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Mar 25, 2009 11:00 am
Location: Ahmedabad

Top

Re: How to write to the File ?

Postby Alexandro » Mon Apr 06, 2009 7:58 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. InputStream is = getYourInputForFileHere();
  3.  
  4. // openFileOutput is called from context. Read more about access levels
  5.  
  6. FileOutputStream out = openFileOutput("myfile.myext",
  7.  
  8.                 Context.MODE_PRIVATE);
  9.  
  10.             byte buf[]=new byte[1024];
  11.  
  12.             int len;
  13.  
  14.             while((len=is.read(buf))>0)
  15.  
  16.                 out.write(buf,0,len);
  17.  
  18.             out.flush();
  19.  
  20.             out.close();
  21.  
  22.             is.close();
  23.  
  24.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

The file will be stored in the specific directory. "data/files" I think.
Alexandro
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 05, 2009 5:42 pm

Postby javame_android » Mon Apr 06, 2009 10:33 am

Hi,

The way you suggested works if the file is being written in the same class that extends Activity class. But if one wants to write the file in different class then it was a problem. So I have written a code that writes to sdcard rather than data/files.

I hope that sdcard would be available in all of the Android Handsets.

Thanks anyways.
Thanks & Regards
Sunil
javame_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Mar 25, 2009 11:00 am
Location: Ahmedabad

Postby Alexandro » Mon Apr 06, 2009 4:26 pm

No problem. You could just provide your current activity to your class.
And I actually not sure, but there maybe are ways to call it some other way.
Alexandro
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 05, 2009 5:42 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests