文件的储存

1.Activity_main.xml

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sh.filepersistentcetest.MainActivity">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="type something here"
        />

</LinearLayout>
2.MainActivity.java
  
public class MainActivity extends AppCompatActivity {
    private EditText edit;
//    private String data="how are you";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit = (EditText)findViewById(R.id.edit);

        String inputText = load();


        if (!TextUtils.isEmpty(inputText))
        {
            edit.setText(inputText);
            edit.setSelection(inputText.length());
            Toast.makeText(this, "restoring sucessed", Toast.LENGTH_SHORT).show();
        }

    }

    public String load()
    {
        FileInputStream in =null;
        BufferedReader reader =null;
        StringBuilder content = new StringBuilder();
        try
        {
           in =openFileInput("inputText");
           reader =   new BufferedReader(new InputStreamReader(in));
           String line="";
           while ((line=reader.readLine()) != null )
           {
               content.append(line);

           }

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally {
              if (reader !=null)
              {
                  try
                  { reader.close();}
                  catch (IOException e)
                  {e.printStackTrace();}
              }
        }
        Toast.makeText(this,content.toString() , Toast.LENGTH_SHORT).show();
        return content.toString();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String inputText = edit.getText().toString();
        save(inputText);
    }

    public  void  save(String inputText)
    {
        BufferedWriter writer =null;
        try {
            //以追加的方式打开文件输出流
            FileOutputStream out = openFileOutput("inputText", MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try{
                if (writer !=null)
                {writer.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
                }
    }


}

猜你喜欢

转载自blog.csdn.net/esucc/article/details/73275164