Save file problem

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

Save file problem

Postby NeoLiu » Wed Jan 09, 2008 7:25 am

Hi all

i had a problem about save file
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         FileOutputStream fOut;
  2.  
  3.         final String TESTSTRING = new String("Hello Android");
  4.  
  5.  
  6.  
  7.                 try {
  8.  
  9.                         fOut = context.openFileOutput("success.txt", context.MODE_WORLD_READABLE);
  10.  
  11.                 } catch (FileNotFoundException e) {
  12.  
  13.                         // TODO Auto-generated catch block
  14.  
  15.                         e.printStackTrace();
  16.  
  17.                 }
  18.  
  19.                 OutputStreamWriter osw = new OutputStreamWriter(fOut);  
  20.  
  21.                 try {
  22.  
  23.                         osw.write(TESTSTRING);
  24.  
  25.                 } catch (IOException e1) {
  26.  
  27.                         // TODO Auto-generated catch block
  28.  
  29.                         e1.printStackTrace();
  30.  
  31.                 }
  32.  
  33.                 //mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  34.  
  35.         /* ensure that everything is
  36.  
  37.          * really written out and close */
  38.  
  39.         try {
  40.  
  41.                 fOut.flush();
  42.  
  43.                 } catch (IOException e) {
  44.  
  45.                         // TODO Auto-generated catch block
  46.  
  47.                         e.printStackTrace();
  48.  
  49.                 }
  50.  
  51.         try {
  52.  
  53.                 fOut.close();
  54.  
  55.                 } catch (IOException e) {
  56.  
  57.                         // TODO Auto-generated catch block
  58.  
  59.                         e.printStackTrace();
  60.  
  61.                 }  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


the FileOutputStream of fOut is null, when openFileOutput(). :(
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Postby bnkhanh » Wed Jan 09, 2008 5:18 pm

I have just build your code. And i found your problem here.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //-----------//
  3.  
  4. try {
  5.  
  6.         fOut = openFileOutput("success.txt",MODE_APPEND);      
  7.  
  8.        OutputStreamWriter osw = new OutputStreamWriter(fOut);
  9.  
  10.         osw.write(TESTSTRING);
  11.  
  12.         osw.flush();
  13.  
  14.         showAlert("ok", "success", "ok", true);
  15.  
  16.         }
  17.  
  18.         catch (Exception e) {      
  19.  
  20.         e.printStackTrace();
  21.  
  22.         showAlert("error", "Error "+ e.toString(), "ok", true);
  23.  
  24.         }
  25.  
  26.         finally {
  27.  
  28.                  try {
  29.  
  30.                     osw.close();
  31.  
  32.                         fOut.close();
  33.  
  34.                         } catch (IOException e) {
  35.  
  36.                         e.printStackTrace();
  37.  
  38.                         }
  39.  
  40.         }
  41.  
  42. //------------------//
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Goodluck to you!
Tran Van Khanh
Da Nang Polytechnic College
bnkhanh
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 07, 2007 8:03 am
Location: Viet Nam

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests