Android下如何读取文件的内容
2016-12-09 13:40
489 查看
唉,尝试了好半天了,才弄懂,1、首先权限的问题我们需要在根文件中添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>2、上面两篇博客可以参考,一个告诉你如何查看文件的绝对路径,一个说明如何借助文件管理器选中文件3、这里我的绝对路径是final String FILE_NAME = "/storage/emulated/0/updatebin/update.txt"; 我把文件在手机里呢4、点击按钮读文件内容,下面两个函数都行btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {dis.setText(readFileSdcardFile(FILE_NAME));dis.setText(readSDFile(FILE_NAME));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}public String readSDFile(String fileName) throws IOException {File file = new File(fileName);FileInputStream fis = new FileInputStream(file);int length = fis.available();byte [] buffer = new byte[length];fis.read(buffer);String res = EncodingUtils.getString(buffer, "UTF-8");fis.close();return res;}
public String readFileSdcardFile(String fileName) throws IOException{String res="";try{FileInputStream fin = new FileInputStream(fileName);int length = fin.available();byte [] buffer = new byte[length];fin.read(buffer);res = EncodingUtils.getString(buffer, "UTF-8");fin.close();}catch(Exception e){e.printStackTrace();}return res;}5、之前的博客里面说的函数也贴过来吧在这个目录下 /storage/emulated/0/updatebin 搜索所有带keyword关键词的文件和文件夹 可以用来查看文件的绝对路径权限:<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>private String searchFile(String keyword) {String result = "";File[] files = new File("/storage/emulated/0/updatebin").listFiles();for (File file : files) {if (file.getName().indexOf(keyword) >= 0) {result += file.getPath() + "\n";}}if (result.equals("")){result = "找不到文件!!";}return result;}6、按钮下 可以调用管理器选择文件 并输出文件信息打开文件管理器选择文件
private void showFileChooser() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("*/*");intent.addCategory(Intent.CATEGORY_OPENABLE);try {startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),FILE_SELECT_CODE);} catch (android.content.ActivityNotFoundException ex) {// Potentially direct the user to the Market with a DialogToast.makeText(MainActivity, "请安装文件管理器", Toast.LENGTH_SHORT).show();}}private void chooseFile() throws IOException {Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("*/*");intent.addCategory(Intent.CATEGORY_OPENABLE);try {startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);} catch (android.content.ActivityNotFoundException ex) {Toast.makeTeb2a1xt(this, "亲,木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show();}}
选择的文件的结果 这里输出文件绝对路径信息
Uri uri = data.getData();Log.i(TAG, "------->" + uri.getPath());@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (resultCode != Activity.RESULT_OK) { super.onActivityResult(requestCode,resultCode, data); return; } if (requestCode == FILE_SELECT_CODE) { Uri uri = data.getData(); Log.i(TAG, "------->" + uri.getPath()); // dis.setText(read()); //readFileOnLine(FILE_NAME); } super.onActivityResult(requestCode, resultCode, data); }7、最后贴上自己完整的代码 按钮点击 打开文件管理器 选择某个文件 并输出文件里面的内容
final String FILE_NAME = "/storage/emulated/0/updatebin/update.txt";
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dis=(TextView) findViewById(R.id.textView1);btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {chooseFile();//按键点击打开文件管理器} catch (IOException e) {e.printStackTrace();}}});}private void chooseFile() throws IOException {Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("*/*");intent.addCategory(Intent.CATEGORY_OPENABLE);try {startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);} catch (android.content.ActivityNotFoundException ex) {Toast.makeText(this, "亲,木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show();}}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif (resultCode != Activity.RESULT_OK) {super.onActivityResult(requestCode, resultCode, data);return;}if (requestCode == FILE_SELECT_CODE) {Uri uri = data.getData();Log.i(TAG, "------->" + uri.getPath());try {//dis.setText(readFileSdcardFile(FILE_NAME));dis.setText(readFileSdcardFile(uri.getPath()));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}super.onActivityResult(requestCode, resultCode, data);}public String readFileSdcardFile(String fileName) throws IOException{String res="";try{FileInputStream fin = new FileInputStream(fileName);int length = fin.available();byte [] buffer = new byte[length];fin.read(buffer);res = EncodingUtils.getString(buffer, "UTF-8");fin.close();}catch(Exception e){e.printStackTrace();}return res;}
相关文章推荐
- 如何读取XML文件内容
- 关于如何使用vc6.0读取xml文件中的内容
- 51.如何使用FileInputStream读取其他文件的内容
- android 读取assets下的文本文件内容
- android 读取raw文件下文件内容
- 求助:C++中读取txt文件如何忽略特定符号后的内容?
- 如何让IE读取本地网页时不提示“Internet Explorer 已经限制此文件显示可能访问您的计算机的活动内容。”
- Android 读取文件内容
- Objective-C学习-如何读取输出文件内容
- 在android应用中读取系统的build.prop文件中的内容
- 请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
- android 读取sd卡文件的内容用TextView显示
- 如何读取csv文件的内容
- android 实用sax 读取xml文件内容
- 如何读取XML文件内容!
- 如何读取csv文件的内容
- android 实用sax 读取xml文件内容
- Android中如何在命令行中查看某个文件的内容
- 如何读取工程中的一个文件内容
- Android如何读取Raw及assets文件夹中文件的数据信息