Android 将数据保存到无法正常工作的文件
我使用以下代码将密码保存在txt文件中:Android 将数据保存到无法正常工作的文件,android,save,Android,Save,我使用以下代码将密码保存在txt文件中: String FILE_NAME="lol.txt"; public void writeData(String password){ FileOutputStream fOut = null; OutputStreamWriter osw = null; try{ fOut = openFileOutput(FILE_NAME, Context.M
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);
}
}
但是,当我检索刚刚使用toast保存的密码时,它显示为OK,但当我将其登录到LogCat时,我得到如下信息:
gana???????????????????????????????????? .... and more four lines.
我使用save方法将gana一词保存到我的文件中,该方法与OnClikc方法一样,将EditText的值设置为密码,如下所示:
public void save(View v){
password= txt.getText().toString();
writeData(password);
}
有什么办法或线索可以解决这个问题吗
关于使用以下代码读取文件
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)
{
}
与您的代码唯一不同的是,我正在使用新的Stringb.trim删除空白。当我检索刚刚使用toast保存的密码时。。。请展示这个代码。谢谢斯里瓦斯塔瓦,我认为我唯一需要做的就是使用Trimm方法来消除空白,让我的代码工作得很好,真的很感激。