數據讀入文件基礎其實在我的應用我有一個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