android之内存操作
来源:互联网 发布:下载物流软件 编辑:程序博客网 时间:2024/05/27 14:13
主显示布局以及代码:
activity_main.xml:
<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" >
<EditText
android:id="@+id/etContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/saveData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存数据"/>
<Button
android:id="@+id/readData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据"/>
</LinearLayout>
java代码:
package com.litsoft.day06_07;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText etContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setListener();
}
private void init() {
// TODO Auto-generated method stub
etContent = (EditText) findViewById(R.id.etContent);
}
private void setListener() {
// TODO Auto-generated method stub
setSaveListener();
findViewById(R.id.readData).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
InputStream in = null;
try {
in = openFileInput("file.dat");
byte[] bs = new byte[1024];
int len = in.read(bs);
String connect = new String (bs,0,len,"utf-8");
etContent.setText(connect);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
}
private void setSaveListener() {
findViewById(R.id.saveData).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
OutputStream out = null;
try {
out = openFileOutput("file.dat", MODE_PRIVATE);
String name = etContent.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(MainActivity.this, "内容必须输入", 2000).show();
return;
}
out.write(name.getBytes());
out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
}
}
具体路径是data--data-包名文件夹--files,在File Explorer下看,具体截图:
效果:
- android之内存操作
- Android读写操作之内存的读写操作
- android 之内存泄漏
- Android之内存管理
- Android之内存管理
- Android之内存泄露
- Android之内存泄露
- Android之内存优化
- vc之内存操作函数
- C语言之内存操作
- C语言之内存操作
- Android开发之内存管理
- Android开发之内存管理
- Android开发之内存管理
- Android开发之内存管理
- Android开发之内存管理
- Android开发之内存管理
- Android基础之内存溢出
- Linux 下使用clock_gettime详解
- <c plus plus concurrency in action>第七章 lock_free 解析 count reference queue
- Linux下/dev/mem和/dev/kmem的区别
- Android 循环加载资源
- Junction.exe 命令应用详解
- android之内存操作
- 列表框空间JList的用法演示
- Android截图命令screencap
- centos 无法发送邮件的解决办法
- libevent
- Android开发学习之Android2D绘图基础
- android canvas 操作 test
- java SSH + extj 项目过程中遇到的问题及注意点(二)
- 黑马程序员——List集合中ArrayList、linkedList、Vector的数据结构及特点