android应用大多数用的java开发,所以java的文件操作在android中完全适用,只不过android对手机而言,多了个外存储设置SD卡。
----------------------------------------------简单的界面布局-----------------------------------------------------------
<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/input_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入保存内容" />
<Button
android:id="@+id/write_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存数据" />
<Button
android:id="@+id/read_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据" />
</LinearLayout>
-----------------------------功能代码-----------------------------------------------------------
public class MainActivity extends Activity implements OnClickListener {
/**内容输入框 */
private EditText inputEdt;
/**保存(写入)按钮 */
private Button writeBtn;
/**读取按钮 */
private Button readBtn;
/**要保存的文件名*/
private static final String FILE_NAME = "temp_file.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
}
private void findViewsById() {
inputEdt = (EditText) findViewById(R.id.input_edt);
writeBtn = (Button) findViewById(R.id.write_btn);
readBtn = (Button) findViewById(R.id.read_btn);
writeBtn.setOnClickListener(this);
readBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.write_btn:
writeData();
break;
case R.id.read_btn:
String result = readData();
inputEdt.setText(result);
break;
}
}
/*
* 方法openFileOutput(FILE_NAME, Activity.MODE_PRIVATE); 参数说明:以FILE_NAME文件名,Activity.MODE_PRIVATE文件的应用模式,打开一个输出流如果文件不存系统会为你创建一个 注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名
* 该文件会被保存在/data/data/应用名称/files/temp_file.txt 文件的操作模式有以下四种:MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 MODE_WORLD_READABLE 公用 可读 MODE_WORLD_WRITEABLE 公用 可读写
*/
private void writeData() {
String content = inputEdt.getText().toString();
try {
FileOutputStream outputStream = openFileOutput(FILE_NAME, Activity.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把刚刚保存的数据读出来
* */
private String readData() {
String content = "";
try {
FileInputStream inputStream = this.openFileInput(FILE_NAME);
byte[] bytes = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (inputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes, 0, bytes.length);
}
inputStream.close();
arrayOutputStream.close();
content = new String(arrayOutputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
/****************上面的内容是把内容存到手机内存中*************************************/
/**判断SDcard是否存在并且可读写*/
private boolean isSDcardExist() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
Toast.makeText(getApplicationContext(), "内存卡不可用", 1000).show();
return false;
}
}
/**往内存卡中存文件*/
public void writeToSDCard(String filename, String filecontent) throws Exception {
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(filecontent.getBytes());
outStream.close();
}
/**读SD卡中对应文件的内容*/
private String readSDCardFile(String filePath, String fileName) {
String result = "";
try {
File file = new File(filePath, fileName);
FileInputStream is = new FileInputStream(file);
byte[] b = new byte[is.available()];
is.read(b);
result = new String(b);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
---------注:-------------------------------------------
SD卡操作不要忘记在AndroidManifest.xml中添加对应权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>