zoukankan      html  css  js  c++  java
  • Android -- 保存文件

    背景                                                                                            

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

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

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

    保存文件                                                                                      

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

    读取文件                                                                                      

    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
            {    
            }    
        }

    主程序                                                                                         

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

                                   我是天王盖地虎的分割线                                 

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

    saveFile.zip

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

  • 相关阅读:
    正则表达式
    js 联系电话验证实现
    curl 解析
    WinRAR打包时不包含文件夹本身,只打包文件夹里的文件和目录
    如何判断事务是否完成,SqlTransaction
    循环枚举的名称或值
    FileStream 和StreamWriter 一起用时
    ToString yyyy-MM-dd ,MM 小写的故事。
    用7z.exe 压缩整个文件夹里的内容
    VS2010安装选项中有个“图标库”
  • 原文地址:https://www.cnblogs.com/yydcdut/p/3708964.html
Copyright © 2011-2022 走看看