Android SD卡内的文件读写操作
2018-07-20 来源:open-open
private String readFromFile(Context context){ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { String foldername = Environment.getExternalStorageDirectory().getPath()+ "/eryaApp"; File folder = new File(foldername); if (folder == null || !folder.exists()) { folder.mkdir(); } File targetFile=new File("/sdcard/eryaApp/eryaShoppingList.txt"); String readedStr=""; try{ if(!targetFile.exists()){ targetFile.createNewFile(); return "No File error "; }else{ InputStream in = new BufferedInputStream(new FileInputStream(targetFile)); BufferedReader br= new BufferedReader(new InputStreamReader(in, "UTF-8")); String tmp; while((tmp=br.readLine())!=null){ readedStr+=tmp; } br.close(); in.close(); return readedStr; } } catch (Exception e) { Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show(); return e.toString(); } }else{ Toast.makeText(context,"未发现SD卡!",Toast.LENGTH_LONG).show(); return "SD Card error"; } }
private void SavedToText(Context context,String stringToWrite){ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { String foldername = Environment.getExternalStorageDirectory().getPath()+ "/eryaApp"; File folder = new File(foldername); if (folder == null || !folder.exists()) { folder.mkdir(); } String fileName="/eryaShoppingList"+".txt"; File targetFile = new File(foldername + fileName); OutputStreamWriter osw; try{ if(!targetFile.exists()){ targetFile.createNewFile(); osw = new OutputStreamWriter(new FileOutputStream(targetFile),"utf-8"); osw.write(stringToWrite); osw.close(); }else{ osw = new OutputStreamWriter(new FileOutputStream(targetFile,true),"utf-8"); osw.write("\n"+stringToWrite); osw.flush(); osw.close(); } } catch (Exception e) { Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show(); } }else{ Toast.makeText(context,"未发现SD卡!",Toast.LENGTH_LONG).show(); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:Android网络相关工具类
最新资讯
热门推荐