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下看,具体截图:


效果:


0 0
原创粉丝点击