数据读入文件基础其实在我的应用我有一个EditText
箱和TextView
。在这个EditText
框中,无论我写的是存储在文件中。因此,当我再次打开我的应用程序时,存储的文本显示在TextView
中。所有的作品都适合我。但是每当我改变俄语的语言时,出现在点击EditText
框中的键盘包含俄语字符。现在,当我按照存储和读取,我输入的文本相同的过程中,而读它读TextView
,而不是说我已经存储在文本一些垃圾值和显示。所以我的问题是为什么只有当我用其他语言存储文本时才会发生这种情况,因为在存储英文语言文本时它工作正常。下面显示了将数据存储到文件时使用的代码。如何编写和不同的语言
写笔记入文件并注册
public void writeNotesToFile(Context c)
{
SharedPreferences preferencesWrite = c.getSharedPreferences("myPreferences", 0);
SharedPreferences.Editor editor = preferencesWrite.edit();
// write notes count into the register
editor.putInt("notesCount", m_noteCount);
editor.commit();
// write notes to the file
SimpleDateFormat sdFormater = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
File file = c.getFileStreamPath("Notes");
if (m_noteCount > 0)
{
if(!file.exists())
{
try
{
file.createNewFile();
}
catch (Exception e)
{
Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
}
}
try
{
FileOutputStream writer = c.openFileOutput("Notes", Context.MODE_PRIVATE);
for (int i = 0; i < m_noteCount; i++)
{
String noteDate = sdFormater.format(m_arrNoteDate[i]);
writer.write(noteDate.getBytes());
writer.write(" ".getBytes());
writer.write(m_arrNoteString[i].getBytes());
writer.write("~`".getBytes());
}
writer.close();
}
catch (Exception e)
{
Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
}
}
else
{
try
{
file.createNewFile();
}
catch (Exception e)
{
Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
}
}
}
从文件中读取笔记和注册
public void readNotesFromFile(Context c)
{
SharedPreferences preferencesRead = c.getSharedPreferences("myPreferences", 0);
// Reads notes count from the register
m_noteCount = preferencesRead.getInt("notesCount", 0);
// Reads notes from file
String note = "";
char nextCharacter;
int count = 0, ch;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
File file = c.getFileStreamPath("Notes");
if (m_noteCount > 0)
{
if(file.exists())
{
try
{
FileInputStream fin = c.openFileInput("Notes");
while((ch = fin.read()) != -1)
{
nextCharacter = (char)ch;
if (nextCharacter == '~')
{
ch = fin.read();
nextCharacter = (char)ch;
if (nextCharacter == '`')
{
int i=note.indexOf(" ");
String temp = note.substring(0, i);
try
{
m_arrNoteDate[count] = formatter.parse(temp);
}
catch (Exception e)
{
// To handle dates saved before the file was written in Local.US format.
// This code can be removed after few releases and all user have migrated.
SimpleDateFormat defFormatter = new SimpleDateFormat("dd-MMM-yyyy");
m_arrNoteDate[count] = defFormatter.parse(temp);
}
m_arrNoteString[count] = note.substring(i + 1);
count++;
note = "";
}
}
else
{
note = note + nextCharacter;
}
}
}
catch (Exception e)
{
Log.i("ReadNWrite, readFile()", "Exception e = " + e);
}
}
}
}
您是否在文件中检查了存储的单词(俄语)与从键盘输入的单词相同。 – 2012-02-29 06:00:32
叶,当它存储的文件存放在我从keyboard..only进入,而读它改变其它字符相同的字符 – AndroidDev 2012-02-29 06:09:09
试试这个tv.setText(Html.fromHtml(your_text_from_file)的ToString());并让我知道它是否有效。 – 2012-02-29 06:19:35