2012-05-04 43 views
0

我用下面的代碼保存密碼在一個txt文件:將數據保存到文件不能正常工作

String FILE_NAME="lol.txt"; 
public void writeData(String password){ 
      FileOutputStream fOut = null; 
      OutputStreamWriter osw = null; 
      try{ 
       fOut = openFileOutput(FILE_NAME, Context.MODE_PRIVATE); 
       osw = new OutputStreamWriter(fOut); 
       osw.write(password); 
       osw.close(); 
       fOut.close(); 
       }catch(Exception e){ 
        e.printStackTrace(System.err); 
       } 
      } 

但是當我retrive,我使用吐司其出現OK剛纔保存的密碼,但是當我登錄它變成我的logcat我有類似以下內容:

gana???????????????????????????????????? .... and more four lines. 

我使用Save方法,其作爲OnClikc方法的工作,設定的EditText的值作爲密碼,如下保存Word加納到我的文件

任何方式或線索我該如何解決這個問題?

問候

+0

'當我檢索我剛剛使用吐司保存的密碼時...請顯示此代碼。 – techiServices

+0

發佈您的閱讀文件代碼。 –

回答

0

使用下面的代碼閱讀文件

byte[] b = null; 
    try 
    { 
     //read file data... 
     FileInputStream myFile = openFileInput("lol.txt"); 
     b = new byte[1024]; 
     myFile.read(b); 
     Log.i("Pass", "File data is: " + new String(b).trim()); 
     myFile.close(); 
    } 
    catch (IOException e) 
    { 
    } 

只與你的代碼不同的是,我使用new String(b).trim()刪除空格。

+0

感謝Srivastava我認爲我唯一需要的是使用trim()方法來刪除我的代碼的空白空間工作正常,真的很感激。 –