Android中文件I/O分为三种方式:SD卡文件读取,手机中文件夹的访问和应用程序汇总Assets文件的读取
将文件存放在SD卡中
public class SimpleActivity extends AppCompatActivity {
private static final String TAG = SimpleActivity.class.getSimpleName();
@Bind(R.id.editText)
EditText editText;
@Bind(R.id.button2)
Button button2;
@Bind(R.id.editText3)
EditText editText3;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
ButterKnife.bind(this);
file = Environment.getExternalStorageDirectory();
Log.i(TAG, "onCreate: "+ file.toString());//I/SimpleActivity: onCreate: /mnt/sdcard
}
@OnClick(R.id.button2)
public void onClick() {
String nr = loadText(editText.getText().toString().trim());
editText3.setText(nr);
}
private String loadText(String name) {
String nr = null;
try {
File f = new File(file.getPath()+"/"+ name);//
Log.i(TAG, "loadText: "+f.getPath());
byte[] buff = new byte[(int) f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(buff);
fis.close();
nr = new String(buff, "utf-8");
nr= nr.replaceAll("\\r\\n","\n");//替换换行符
}catch (Exception e){
e.printStackTrace();
Toast.makeText(SimpleActivity.this, "对不起没有找到指定文件", Toast.LENGTH_SHORT).show();
}
return nr;
}
}
将文件放在手机内存中(ROM相当于PC的硬盘)
public class SimpleActivity extends Activity {
private static final String TAG = SimpleActivity.class.getSimpleName();
@Bind(R.id.listView)
ListView listView;
@Bind(R.id.textView2)
TextView currDirTV;//显示当前路径的textview应用
private File file;
String currPath;//当前路径字符串
String rootPath = "/";//根目录路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
ButterKnife.bind(this);
File[] files = getFiles(rootPath);
currPath = rootPath;
currDirTV.setText("当前路径:" + currPath);
initListView(files, listView);
}
private void initListView(File[] files, ListView listView) {
List<String> fileStr = new ArrayList<>();
for(File file:files){
fileStr.add(file.toString());
Log.i(TAG, "initListView: "+file.toString());
//I/SimpleActivity: initListView: /sdcard
// I/SimpleActivity: initListView: /persist
}
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,fileStr));
}
/**
* 获取当前目录下的文件列表的方法
*
* @param rootPath
* @return
*/
private File[] getFiles(String rootPath) {
File[] files = new File(rootPath).listFiles();//获取当前目录下的文件列表
return files;
}
}
在assets目录下的文件读取
public class SimpleActivity extends Activity {
private static final String TAG = SimpleActivity.class.getSimpleName();
@Bind(R.id.editText)
EditText editText;
@Bind(R.id.button2)
Button button2;
@Bind(R.id.editText2)
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
ButterKnife.bind(this);
}
@OnClick(R.id.button2)
public void onClick() {
String nr = loadText(editText.getText().toString().trim());
Log.i(TAG, "onClick: "+editText.getText().toString().trim());
editText2.setText(nr);
}
private String loadText(String name) {
String nr = null;
try {
InputStream is = this.getResources().getAssets().open(name);
int ch = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((ch = is.read()) != -1) {
baos.write(ch);//read file
}
byte[] buff = baos.toByteArray();//换成字节数组
baos.close();
is.close();
nr = new String(buff, "utf-8");
nr = nr.replaceAll("\\r\\n", "\n");
} catch (Exception e) {
Toast.makeText(SimpleActivity.this, "sorry, not find the file!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return nr;
}
}
我们的自己建的文件夹改放什么位置,在项目名.iml(我的是app.iml)