Android 儲存檔案

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

背景                                                                                            

我們以常見的登入的時候有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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.