默认本工程创建的文件本工程对其有读写权限。
我们可以通过context.openFileOutput("文件名", 模式);
我们可以创建私有, 共有, 只读, 只写文件, 默认的文私有文件。
如果别的Android工程访问本工程的文件的话就会受限制, android的内核是linux, 所以他的文件管理和linux中的文件时一样的。
创建文件代码:
/**
* 创建各种文件
* @throws IOException
*
*/
@SuppressLint({ "WorldWriteableFiles", "WorldReadableFiles" })
@SuppressWarnings("resource")
private void createFile() throws IOException {
Log.i(TAG, "开始创建文件"); FileOutputStream fos; //创建私有文件
fos = this.openFileOutput("private.txt", Context.MODE_PRIVATE);
fos.write("private".getBytes());
fos.close(); //创建公有文件
fos = this.openFileOutput("public.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
fos.write("public".getBytes());
fos.close(); //创建默认文件
File file = new File(this.getFilesDir(), "default.txt");
fos = new FileOutputStream(file);
fos.write("default".getBytes());
fos.close(); //创建只读文件
fos = this.openFileOutput("readable.txt", Context.MODE_WORLD_READABLE);
fos.write("readable".getBytes());
fos.close(); //创建只写文件
fos = this.openFileOutput("writeable.txt", Context.MODE_WORLD_WRITEABLE);
fos.write("writeable".getBytes());
fos.close(); Toast.makeText(this, "文件创建成功", Toast.LENGTH_LONG).show(); Log.i(TAG, "创建文件完成");
}
othera Android工程访问 filespower工程所创建的文件
package com.example.other; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener{ private String TAG = "MainActivity"; /** 读写私有文件 */
private Button btnWritePrivate, btnReadPrivate; /** 读写共有文件 */
private Button btnWritePublic, btnReadPublic ; /** 读写默认文件 */
private Button btnWriteDefalut, btnReadDefault; /** 读写只读文件 */
private Button btnWriteReadable, btnReadReadable; /** 读写只写文件 */
private Button btnWriteWriteable, btnReadWriteable; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //组件获取
btnWritePrivate = (Button)findViewById(R.id.btn_writePrivate);
btnReadPrivate = (Button)findViewById(R.id.btn_readPrivate); btnWritePublic = (Button)findViewById(R.id.btn_writePublic);
btnReadPublic = (Button)findViewById(R.id.btn_readPublic); btnWriteDefalut = (Button)findViewById(R.id.btn_writeDefault);
btnReadDefault = (Button)findViewById(R.id.btn_readDefault); btnWriteReadable = (Button)findViewById(R.id.btn_writeReadable);
btnReadReadable = (Button)findViewById(R.id.btn_readReadable); btnWriteWriteable = (Button)findViewById(R.id.btn_writeWriteable);
btnReadWriteable = (Button)findViewById(R.id.btn_readWriteable); //设置监听事件
btnWritePrivate.setOnClickListener(this);
btnReadPrivate.setOnClickListener(this); btnWritePublic.setOnClickListener(this);
btnReadPublic.setOnClickListener(this); btnWriteDefalut.setOnClickListener(this);
btnReadDefault.setOnClickListener(this); btnWriteReadable.setOnClickListener(this);
btnReadReadable.setOnClickListener(this); btnWriteWriteable.setOnClickListener(this);
btnReadWriteable.setOnClickListener(this); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_readPrivate:
readFile("private.txt");
break;
case R.id.btn_readPublic:
readFile("public.txt");
break;
case R.id.btn_readDefault:
readFile("default.txt");
break;
case R.id.btn_readReadable:
readFile("readable.txt");
break;
case R.id.btn_readWriteable:
readFile("writeable.txt");
break;
case R.id.btn_writePrivate:
writeFile("private.txt");
break;
case R.id.btn_writePublic:
writeFile("public.txt");
break;
case R.id.btn_writeDefault:
writeFile("default.txt");
break;
case R.id.btn_writeReadable:
writeFile("readable.txt");
break;
case R.id.btn_writeWriteable:
writeFile("writeable.txt");
break; default:
break;
} } @SuppressLint("SdCardPath")
public void readFile(String fileName) {
Log.i(TAG, "读文件"+fileName); File file = new File("/data/data/com.example.filepower/files/"+fileName);
try {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(file));
String context = br.readLine();
Toast.makeText(this, context, Toast.LENGTH_SHORT).show(); } catch (Exception e) {
Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
} } @SuppressLint("SdCardPath")
@SuppressWarnings("resource")
public void writeFile(String fileName) {
Log.i(TAG, "写文件"+fileName);
File file = new File("/data/data/com.example.filepower/files/", fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write("xxx".getBytes());
} catch (Exception e) {
Toast.makeText(this, "写文件失败", Toast.LENGTH_SHORT).show();
}
}
}
文件讲解: