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

Android-读取文件数据

2015-04-12 11:12 453 查看
读取Assets中的数据

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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: