Android JAVA流 输入输出

private void save(){
    FileOutputStream out;
    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();
                    showToast("Output succeed!");

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

private void load(){
    FileInputStream in;
    BufferedReader reader=null;
    StringBuilder content=new StringBuilder();
    String line="";
    try {
        in=openFileInput("data");
        reader=new BufferedReader(new InputStreamReader(in));
        while((line=reader.readLine())!=null){
            content.append(line);
        }
        showToast(content.toString());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if(reader!=null){
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37471638/article/details/81327433