如何使用不同的语言将数据写入和读取到文件中

时间:2012-02-29 05:08:51

标签: android

实际上,在我的应用中,我有一个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);

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更新了解决问题的代码

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 
        {
                      //convert to UTF format while Writing Data
            FileOutputStream fos = new FileOutputStream(file);
            Writer writer = new OutputStreamWriter(fos, "UTF8");

               // 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);
                writer.write(" ");
                writer.write(m_arrNoteString[i]);
                writer.write("~`");

            }
            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 
            {
                                  //convert to UTF format while Reading Data
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader fin = new InputStreamReader(fis, "UTF8");

                // 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);

            }
        }
    }
}