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