Android之内部存储读取数据

<LinearLayout 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" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/et"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="5"
            android:hint="请输入数据"
            android:singleLine="true" />


        <Button
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="3"
            android:onClick="click"
            android:text="保存数据" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <TextView
            android:id="@+id/tv"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="5"
            android:hint="输出数据"
            android:singleLine="true" />


        <Button
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="3"
            android:onClick="click2"
            android:text="读取数据" />
    </LinearLayout>


</LinearLayout>



xml中布局,设置保存读取按钮。


public class MainActivity extends Activity {


private EditText text;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


text = (EditText) findViewById(R.id.et);
}


public void click(View v) {


Toast.makeText(this, "保存数据中", 1).show();


String data = text.getText().toString();


save(data, "test.txt");


}


// 保存数据data到filename中
private void save(String data, String filename) {
// TODO Auto-generated method stub


File file = new File(getFilesDir(), filename);


if (file.exists()) {


FileOutputStream output;


try {
output = new FileOutputStream(file);


output.write(data.getBytes());


output.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public void click2(View v) {
// TODO Auto-generated method stub


TextView tv = (TextView) findViewById(R.id.tv);
Toast.makeText(this, "读取数据中", 1).show();


String tv_data = read( );

Log.d("Qishui",tv_data);
tv.setText(tv_data);


}


private String read() {
// TODO Auto-generated method stub
File file = new File(getFilesDir(), "test.txt");


String data = null;


if (file.exists()) {


FileInputStream input;


try {
input = new FileInputStream(file);


BufferedReader br = new BufferedReader(new InputStreamReader(input));


data = br.readLine();


input.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;


}


}





注意:不需要权限!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值