安卓内部存储

java文件 

package edu.hrbeu.InternalFileDemo;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


public class InternalFileDemoActivity extends Activity {
    
private final String FILE_NAME = "fileDemo.txt";
private TextView labelView;
private TextView displayView;
private CheckBox appendBox ;
private EditText entryText;
private EditText edit1;
private EditText edit2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        labelView = (TextView)findViewById(R.id.label);
        displayView = (TextView)findViewById(R.id.display);
        appendBox = (CheckBox)findViewById(R.id.append);
    entryText = (EditText)findViewById(R.id.entry);
    edit1=(EditText)findViewById(R.id.editText1);
    edit2=(EditText)findViewById(R.id.editText2);
        Button writeButton = (Button)findViewById(R.id.write);
        Button readButton = (Button)findViewById(R.id.read);
        writeButton.setOnClickListener(writeButtonListener);
        readButton.setOnClickListener(readButtonListener);
        entryText.selectAll();
        entryText.findFocus();
    }
    
    
    OnClickListener writeButtonListener = new OnClickListener() { 
@Override
public void onClick(View v) {
FileOutputStream fos = null;
    try {
    if (appendBox.isChecked()){
fos = openFileOutput(FILE_NAME,Context.MODE_APPEND);
}
else {
fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
}
         
    String text = entryText.getText().toString()+"  "+edit1.getText().toString()+"  "+edit2.getText().toString();
    fos.write(text.getBytes());
    //labelView.setText("文件写入成功,写入长度:"+text.length());
    entryText.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
if (fos != null){
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}   
     };
     
     OnClickListener readButtonListener = new OnClickListener() { 
  @Override
  public void onClick(View v) {
  displayView.setText("");
  FileInputStream fis = null;
  try {
fis = openFileInput(FILE_NAME);
if (fis.available() == 0){
return;
}
byte[] readBytes = new byte[fis.available()];
while(fis.read(readBytes) != -1){
}
String text = new String(readBytes);
displayView.setText(text);
//labelView.setText("文件读取成功,文件长度:"+text.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
 
  }   
      };
     
}

布局文件 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


<TextView
    android:id="@+id/label"
    android:layout_width="74dp"
    android:layout_height="wrap_content"
    android:text="姓名" />


<EditText
    android:id="@+id/entry"
    android:layout_width="170dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="输入文件内容" >


    <requestFocus />
</EditText>


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="学号" />


<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">


<EditText
    android:id="@+id/editText1"
    android:layout_width="178dp"
    android:layout_height="wrap_content" />
</LinearLayout>


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="班级" />


<EditText
    android:id="@+id/editText2"
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:ems="10" />


<Button
    android:id="@+id/read"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="读取文件" />


<Button
    android:id="@+id/write"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="写入文件" />


<CheckBox android:id="@+id/append"
android:text="追加模式"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</CheckBox>
<TextView android:id="@+id/display"
android:text="文件内容显示区域"  
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="#FFFFFF" 
android:textColor="#000000" >
</TextView>
</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值