Android开发——数据存储之文件存储
2015-03-28 08:57
309 查看
通过openFileOutput和openFileInputput方法现。
写文件使用OutStream,可以通过openFileOutput方法 获得OutStream对象。
写文件使用InStream,可以通过openFileInput方法 获得InStream对象。
例子:
程序清单1
File.java
package xiaocool.net.memory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by lrenovo G480 on 2015/3/21.
*/
public class FileActivity extends Activity {
private Button bt1,bt2;
private EditText et1,et2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file);
bt1 = (Button) this.findViewById(R.id.button);
bt2 = (Button) this.findViewById(R.id.button2);
et1 = (EditText) this.findViewById(R.id.editText);
et2 = (EditText) this.findViewById(R.id.editText2);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
write(et1.getText().toString());
Toast.makeText(FileActivity.this, "文件写入成功", Toast.LENGTH_LONG)
.show();
}
});
bt2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
et2.setText(read());
Toast.makeText(FileActivity.this, "文件读取成功", Toast.LENGTH_LONG)
.show();
}
});
}
private void write(String str) {
try {
OutputStream a = openFileOutput("file.txt", Activity.MODE_PRIVATE);
a.write(str.getBytes());
a.close();
}
catch (Exception e) {
}
}
private String read() {
try {
InputStream inputStream = openFileInput("file.txt");
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
return new String(b);
}
catch (Exception e) {
}
return null;
}
}
程序清单2
file.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写文件"
android:id="@+id/button"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText2"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读文件"
android:id="@+id/button2"
android:layout_marginTop="50dp"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />
</RelativeLayout>
效果图:
通过openFileOutput和openFileInputput方法现。
写文件使用OutStream,可以通过openFileOutput方法 获得OutStream对象。
写文件使用InStream,可以通过openFileInput方法 获得InStream对象。
例子:
程序清单1
File.java
package xiaocool.net.memory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by lrenovo G480 on 2015/3/21.
*/
public class FileActivity extends Activity {
private Button bt1,bt2;
private EditText et1,et2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file);
bt1 = (Button) this.findViewById(R.id.button);
bt2 = (Button) this.findViewById(R.id.button2);
et1 = (EditText) this.findViewById(R.id.editText);
et2 = (EditText) this.findViewById(R.id.editText2);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
write(et1.getText().toString());
Toast.makeText(FileActivity.this, "文件写入成功", Toast.LENGTH_LONG)
.show();
}
});
bt2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
et2.setText(read());
Toast.makeText(FileActivity.this, "文件读取成功", Toast.LENGTH_LONG)
.show();
}
});
}
private void write(String str) {
try {
OutputStream a = openFileOutput("file.txt", Activity.MODE_PRIVATE);
a.write(str.getBytes());
a.close();
}
catch (Exception e) {
}
}
private String read() {
try {
InputStream inputStream = openFileInput("file.txt");
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
return new String(b);
}
catch (Exception e) {
}
return null;
}
}
程序清单2
file.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写文件"
android:id="@+id/button"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText2"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读文件"
android:id="@+id/button2"
android:layout_marginTop="50dp"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />
</RelativeLayout>
效果图:
相关文章推荐
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- Android开发手记(17) 数据存储二 文件存储数据
- Android开发必备技能之Android数据存储(文件存储篇)
- 详解Android开发数据持久化之文件存储(附源码)
- Android开发--数据存储之File文件存储
- (转)【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android官方开发文档Training系列课程中文版:数据存储之文件存储
- Android游戏开发13:SQLite存储数据及保存SQLite数据库文件到SD卡
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- android开发 解析服务器端xml文件数据存储到android客户端SQLite数据库
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android开发--数据存储之File文件存储
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- android 开发-数据存储之文件存储
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!