向SD卡写内容并且读SD内容

public void Write(View view){
    try {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
            OutputStream os = new FileOutputStream(file);
            os.write("万薪就业,高薪就业".getBytes());
            os.flush();
            os.close();
        }else {
            Toast.makeText(this,"没写入",Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void Read(View view){
    try {
        File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
        InputStream is = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = br.readLine();
        is.close();
        br.close();
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();

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

}

你可能感兴趣的:(向SD卡写内容并且读SD内容)