Create/Read File

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

Create/Read File

Postby felix » Mon Mar 17, 2008 6:13 pm

Well,

I'm reading about the files manipulation in android, but all methods what I found not works. In the DDMS perspective i'm not see the files folder, and i've tried to read the file, but not work, see my code above:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                  FileOutputStream fOut = openFileOutput("a.txt",
  3.  
  4.                                           MODE_WORLD_READABLE);
  5.  
  6.                  OutputStreamWriter osw = new OutputStreamWriter(fOut);
  7.  
  8.  
  9.  
  10.                  // Write the string to the file
  11.  
  12.                  osw.write("Hello World!");
  13.  
  14.                  /* ensure that everything is
  15.  
  16.                   * really written out and close */
  17.  
  18.                  osw.flush();
  19.  
  20.                  osw.close();
  21.  
  22.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This is throws a FileNotFoundException, I've tried to read a file, and it not work

Thanks for your help
felix
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Feb 14, 2008 4:25 pm
Location: Brasil

Top

file writing

Postby cadlg » Mon Mar 17, 2008 7:06 pm

Hi felix.

If that exception is shown at design time (not really thrown as an exception at run time), you just need to place your code inside a try-catch block.

If that's the case, try tomething like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             FileOutputStream fOut;
  2.  
  3.             OutputStreamWriter osw;
  4.  
  5.            
  6.  
  7.             try {
  8.  
  9.                 fOut = openFileOutput("a.txt",MODE_WORLD_READABLE);
  10.  
  11.             } catch (FileNotFoundException e1) {
  12.  
  13.                 e1.printStackTrace(); // or do something else
  14.  
  15.                 fOut = null;
  16.  
  17.             }
  18.  
  19.                 osw = new OutputStreamWriter(fOut);
  20.  
  21.             try {
  22.  
  23.                 //      Write the string to the file
  24.  
  25.                 osw.write("Hello World!");
  26.  
  27.                 /* ensure that everything is
  28.  
  29.                  * really written out and close */
  30.  
  31.                 osw.flush();
  32.  
  33.                 osw.close();            
  34.  
  35.             } catch (IOException e1) {
  36.  
  37.                 e1.printStackTrace(); // or do something else
  38.  
  39.             }
  40.  
  41.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby felix » Mon Mar 17, 2008 7:22 pm

I'm have a try/catch, i'm not put is here.

This raise an FileNotFoundException in the fOut = openFileOutput("a.txt",MODE_WORLD_READABLE);

thanks for the reply
felix
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Feb 14, 2008 4:25 pm
Location: Brasil

Postby kiran » Tue Mar 18, 2008 8:45 am

Hi,

This works for me.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.  
  3.                         FileOutputStream fOut = openFileOutput("samplefile.txt",MODE_WORLD_READABLE);
  4.  
  5.                         OutputStreamWriter osw = new OutputStreamWriter(fOut);
  6.  
  7.                         try {
  8.  
  9.                                 osw.write("HelloAndroid");
  10.  
  11.                                 osw.flush();
  12.  
  13.                     osw.close();
  14.  
  15.                         } catch (IOException e) {
  16.  
  17.                                 e.printStackTrace();
  18.  
  19.                         }
  20.  
  21.                        
  22.  
  23.                 } catch (FileNotFoundException e) {
  24.  
  25.                         e.printStackTrace();
  26.  
  27.                 }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
:) smile it is the key that fits the lock of everybody's heart
User avatar
kiran
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Thu Nov 22, 2007 11:36 am
Location: India

Postby felix » Tue Mar 18, 2008 12:18 pm

it's not work,

it's catch FileNotFound, can't create a file, I need to create this file? how to create?

thanks
felix
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Feb 14, 2008 4:25 pm
Location: Brasil

Postby Jesmiatka » Tue Mar 18, 2008 1:48 pm

He said it does work for his emulator ;-)

Try running the emulator with -wipe-data command and use the code that kiran used.
If that didn't solve it, post again ;-)
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], MSNbot Media and 21 guests