Android 保存文件

时间:2014-05-10 00:14:22   收藏:0   阅读:439

背景                                                                                            

我们以常见的登录的时候有CheckBox来显示是否保存帐号密码的形式来生动的讲解这个故事。

最后是以txt文档保存,用到的是Java的IO操作。

这个只是粗略的,大家不喜勿喷。

保存文件                                                                                      

bubuko.com,布布扣
public static void savefile2card(Context context,String username,String password)
    {
        File file = null;
        FileOutputStream fos = null;
        try 
        {
        //    file = new File("/data/data/com.yuyidong.savefile/savefile.txt");
            file = new File(context.getFilesDir(),"info.txt");
            fos = new FileOutputStream(file);
            fos.write((username+"!!!!"+password).getBytes());    
        } 
        catch (Exception e) 
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            try 
            {
                fos.close();
            } 
            catch (IOException e1) 
            {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        }
    }
bubuko.com,布布扣

读取文件                                                                                      

bubuko.com,布布扣
public static Map<String,String> getSaveFile(Context context)
    {
        File file =new File(context.getFilesDir(),"info.txt");        
        try 
        {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String str = br.readLine();
            String[] infos = str.split("!!!!");
            Map<String,String> map = new HashMap<String, String>();
            map.put("username",infos[0]);
            map.put("password", infos[1]);
            br.close();
            return map;
        } 
        catch (Exception e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            return null;
        }
        finally
        {    
        }    
    }
bubuko.com,布布扣

主程序                                                                                         

bubuko.com,布布扣
public class MainActivity extends Activity {

    private Button button;
    private CheckBox check;
    private EditText usernameText;
    private EditText passwordText;
    private String username;
    private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        check = (CheckBox) findViewById(R.id.check);

        usernameText = (EditText) findViewById(R.id.username);        
        passwordText = (EditText) findViewById(R.id.password);
    
        button.setOnClickListener(new buttonListener());

        Map<String, String> map = read.getSaveFile(this);
        usernameText.setText(map.get("username"));
        passwordText.setText(map.get("password"));
    }
    
    class buttonListener implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            username = usernameText.getText().toString();
            password = passwordText.getText().toString();
            System.out.println(username+"~!!~"+password);
            if(check.isChecked())
            {                
                save.savefile2card(MainActivity.this, username, password);
            }
        }
    }
bubuko.com,布布扣

                               我是天王盖地虎的分割线                                 

源代码:http://pan.baidu.com/s/1dD1Qx01

saveFile.zip

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3708964.html

Android 保存文件,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!