文件的简单读写操作
用openFileOutput()方法保存的文件,是直接保存在手机空间中的,一般适合保存较小的文件;
写入:
1 public boolean writeFile(String fileName, String content) { 2 try { 3 FileOutputStream stream = openFileOutput(fileName, MODE_PRIVATE); 4 byte[] buffers = content.getBytes(); 5 6 stream.write(buffers); 7 stream.flush();//---清空缓存 8 stream.close(); 9 return true; 10 } 11 catch (Exception ex) { 12 ex.printStackTrace(); 13 return false; 14 } 15 }
读出:
1 public String readFile(String fileName) { 2 String content = ""; 3 try { 4 FileInputStream stream = openFileInput(fileName); 5 int length = stream.available(); 6 byte[] buffers = new byte[length]; 7 8 stream.read(buffers); 9 content = new String(buffers); 10 } 11 catch(Exception ex) { 12 ex.printStackTrace(); 13 } 14 return content; 15 }