【发布时间】:2012-02-29 05:08:50
【问题描述】:
实际上,在我的应用程序中,我有一个 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);
}
}
}
}
【问题讨论】:
-
您是否签入了存储的(俄语)单词与您从键盘输入的单词相同的文件。
-
是的,当它存储文件时,它存储在我从键盘输入的相同字符中......只有在读取它时才会更改为其他字符
-
试试这个 tv.setText(Html.fromHtml(your_text_from_file).toString());让我知道它是否有效。
-
实际上在从文件中读取数据时会产生问题...而不是读取我写入文件的字符它读取不同的垃圾字符..但是如果语言是英语它会读取正确的字符
-
不,它不起作用..与“UTF8”格式有什么关系..但我不知道。
标签: android