您的位置:首页 > 移动开发 > Android开发

Android下如何读取文件的内容

2016-12-09 13:40 393 查看
唉,尝试了好半天了,才弄懂,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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: