Android Studio文件读写操作

Android Studio文件读写操作

自己学习记录,以便后续复习和查找。大神飘过!
直接上代码
读代码:

 public String read(){FileInputStream in=null;BufferedReader reader=null;StringBuilder content=new StringBuilder();try{in=openFileInput("data");reader=new BufferedReader(new InputStreamReader(in));String line="";while ((line=reader.readLine())!=null){content.append(line);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(reader!=null){try {reader.close();}catch (IOException e){e.printStackTrace();}}}return content.toString();}

写代码:

public void save(){String data="Data to save";FileOutputStream out=null;BufferedWriter writer=null;try{out=openFileOutput("data",Context.MODE_PRIVATE);writer =new BufferedWriter(new OutputStreamWriter(out));writer.write(data);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if (writer!=null){writer.close();}}catch (IOException e){e.printStackTrace();}}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部