Android-读取文件数据
2015-04-12 11:12
453 查看
读取Assets中的数据
读取Raw文件下的数据
读写内部存储的文件数据
程序运行后,会在系统的data目录下的data文件夹中以当前程序的包名创建一个文件夹。这个内部的存储空间仅仅对当前应用可见。
使用openFileOutput()方法获取内部文件的输出流与文件数据的写入
使用openFileInput()获取内部文件的输入流并将数据读取出来。
读写外部存储的文件数据
读写外部存储的文件数据,主要指读写SD卡中的数据。使用Environment.getExternalStorageDirectory()获取系统SD卡路径,并使用File类进行后续的操作。
写入数据
读取数据
InputStream is = getResources().getAssets().open("info.txt"); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader bfr = new BufferedReader(isr); String in = ""; while((in = bfr.readLine()) != null){ Log.i(TAG_STRING, in); }
读取Raw文件下的数据
try { InputStream is = getResources().openRawResource(R.raw.info); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader bfr = new BufferedReader(isr); String in = ""; try { while((in = bfr.readLine()) != null){ Log.i(TAG, in); } } catch (IOException e) { e.printStackTrace(); } } catch (NotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
读写内部存储的文件数据
程序运行后,会在系统的data目录下的data文件夹中以当前程序的包名创建一个文件夹。这个内部的存储空间仅仅对当前应用可见。
使用openFileOutput()方法获取内部文件的输出流与文件数据的写入
try { FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);//将文件数据写入到应用的内部存储 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.write(editText.getText().toString()); osw.flush(); fos.flush(); osw.close(); fos.close(); Toast.makeText(getApplicationContext(), "写入完成", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
使用openFileInput()获取内部文件的输入流并将数据读取出来。
try { FileInputStream fis = openFileInput(fileName);//用来直接读取应用程序内部空间的文件内容 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");//字节流转换成字符流 char input[] = new char[fis.available()]; isr.read(input); isr.close(); fis.close(); String readed = new String(input); showTextView.setText(readed); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
读写外部存储的文件数据
读写外部存储的文件数据,主要指读写SD卡中的数据。使用Environment.getExternalStorageDirectory()获取系统SD卡路径,并使用File类进行后续的操作。
写入数据
File sdcard = Environment.getExternalStorageDirectory(); File myFile = new File(sdcard, "This is my File.txt"); if (!sdcard.exists()) {//存在SD卡 Toast.makeText(getApplicationContext(), "当前系统不具备SD卡目录", Toast.LENGTH_SHORT).show(); return; } try { myFile.createNewFile(); Toast.makeText(getApplicationContext(), "文件已经创建完成", Toast.LENGTH_SHORT).show(); FileOutputStream fos = new FileOutputStream(myFile); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.write(editText.getText().toString()); osw.flush(); osw.close(); fos.close(); Toast.makeText(getApplicationContext(), "文件已经写入完成", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); }
读取数据
File sdcard = Environment.getExternalStorageDirectory(); File myFile = new File(sdcard, "This is my File.txt"); if (myFile.exists()) { try { FileInputStream fis = new FileInputStream(myFile); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); char[] input = new char[fis.available()]; isr.read(input); isr.close(); fis.close(); String inString = new String(input); textView.setText(inString); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- android往文件中保存和读取数据
- Android 数据存储与读取:文件
- android往文件中保存和读取数据
- Android如何读取Raw及assets文件夹中文件的数据信息
- Android 读取文件数据
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- Android入门学习笔记(一)|基础知识|文件数据存储读取|解析XML
- Android 读取Manifest文件下Application 等节点下的meta-data自定义数据
- Android 之 使用File类在SD卡中读取数据文件
- Android 之 使用File类在SD卡中读取数据文件
- android 打开pdf文件 从inputStream读取数据并保存为文件
- Android读取assets目录下文件数据内容
- Android 读取清单文件<meta-data>元素的数据
- Android 读取清单文件<meta-data>元素的数据
- android之读取xml文件中的数据和创建
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- Android读取assets目录下文件数据内容
- Android读取assets目录下文件数据内容
- android app 读取本地.db文件,实现不变数据本地化(附小demo)
- Android开发之从文件中读取数据