sd卡写入读取

public class MainActivity extends AppCompatActivity {

    private BufferedReader sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_main_write= (Button) findViewById(R.id.btn_main_write);
        Button btn_main_read = (Button) findViewById(R.id.btn_main_read);
        btn_main_write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    saveToSDCard("/bawei.txt","万新就业,高新就业");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        btn_main_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String s = readsf();
                Toast.makeText(MainActivity.this,"============"+s,Toast.LENGTH_SHORT).show();
            }
        });


    }
    public void saveToSDCard(String filename,String content) throws Exception{
        File file=new File(Environment.getExternalStorageDirectory()+filename);
        FileWriter out= new FileWriter(file);
        out.write(content);
        Toast.makeText(MainActivity.this,"写入成功",Toast.LENGTH_SHORT).show();
        out.close();
    }
    private String readsf(){
        File file=new File(Environment.getExternalStorageDirectory(),"/bawei.txt");
        try {

            FileReader inputStream= new FileReader(file);
            sb = new BufferedReader(inputStream);
            return  sb.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}

你可能感兴趣的:(sd卡写入读取)