读:
Android从resource中的raw文件夹中获取文件并读取数据:
final Resources resources = getResources();InputStream inputStream = resources.openRawResource(R.raw.area_card);
Android读取Asset里的内容
InputStream inputStream = getResources().getAssets().open("xml/Violate_dice.xml");
Android从SDCard读取文件
String fileName = "/sdcard/Y.txt";
//也可以用String fileName = "mnt/sdcard/Y.txt";
FileInputStream fis = new FileInputStream(fileName);
Android读取data/data/com.xxx.app/files/下的文件
String fileName = "txt/Y.txt"; //Y.txt实际应该存在在/data/data/com.xxx.app/files/txt/Y.txt
FileInputStream fin = openFileInput(fileName);
--------------------------------------------------------------------
写:
Android写文件到/data/data/com.xxx.app/files/String message = "this is a string";
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
Android写文件到SDCard
String fileName = "mnt/sdcard/Y.txt";
String message = "this is a string";
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
Android向系统节点写内容
try {
BufferedWriter output = new BufferedWriter(new FileWriter(new File("/sys/power/state")));
output.write("mem");
output.close();
} catch (Exception e) {
e.printStackTrace();
}