欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android实现文件存储案例

程序员文章站 2022-06-18 13:24:57
本文实例为大家分享了android实现文件存储的具体代码,供大家参考,具体内容如下1、文件存储案例public class testactivity extends appcompatactivity...

本文实例为大家分享了android实现文件存储的具体代码,供大家参考,具体内容如下

1、文件存储案例

public class testactivity extends appcompatactivity {
    private edittext mfileedit;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_test);
        initview();
    }

    private void initview() {
        mfileedit = findviewbyid(r.id.fileedit);
        string inputtext = load();
        if (!textutils.isempty(inputtext)) {
            mfileedit.settext(inputtext);
            mfileedit.setselection(inputtext.length());
            toast.maketext(this, "restoring succeeded", toast.length_short).show();
        }
    }


    @override
    protected void ondestroy() {
        super.ondestroy();
        string inputtext = mfileedit.gettext().tostring();
        save(inputtext);
    }

    //    从文件中读取数据
    public void save(string inputtext) {
        fileoutputstream outputstream = null;
        bufferedwriter writer = null;
        try {
            outputstream = openfileoutput("data", context.mode_private);
            writer = new bufferedwriter(new outputstreamwriter(outputstream));
            writer.write(inputtext);
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }

    //    将文件存储到文件中
    public string load() {
        fileinputstream inputstream = null;
        bufferedreader reader = null;
        stringbuilder builder = new stringbuilder();
        try {
            inputstream = openfileinput("data");
            reader = new bufferedreader(new inputstreamreader(inputstream));
            string line = "";
            while ((line = reader.readline()) != null) {
                builder.append(line);
            }
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
        return builder.tostring();
    }
}

运行结果,pass

2、sharepreferences存储案例

public class sharepfsactivity extends appcompatactivity implements view.onclicklistener {
    private static final string tag = "sharepfsactivity";
    private button mshareddata;
    private button mrestoredata;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_shared_pfs);
        initview();
    }

    private void initview() {
        mshareddata = findviewbyid(r.id.sharedbtn);
        mshareddata.setonclicklistener(this);
        mrestoredata = findviewbyid(r.id.restorebtn);
        mrestoredata.setonclicklistener(this);
    }

    @override
    public void onclick(view view) {
        switch (view.getid()) {
            case r.id.sharedbtn:
                shareddata();
                break;
            case r.id.restorebtn:
                restoredata();
                break;
            default:
                break;
        }
    }

    private void shareddata() {
        sharedpreferences.editor editor = getsharedpreferences("sharedata", mode_private).edit();
        editor.putstring("name", "功勋");
        editor.putstring("type", "电影");
        editor.apply();
    }

    private void restoredata() {
        sharedpreferences preferences = getsharedpreferences("sharedata", mode_private);
        string name = preferences.getstring("name", "");
        string type = preferences.getstring("type", "");
        log.d(tag, "名称:" + name + ",类型:" + type);
    }
}

运行结果,pass

Android实现文件存储案例

3、登录页面,实现记住username和pwd功能

activity_login.xml文件

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <linearlayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:" />

        <edittext
            android:id="@+id/username"
            android:layout_width="240dp"
            android:layout_height="wrap_content" />
    </linearlayout>

    <linearlayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:" />

        <edittext
            android:id="@+id/pwd"
            android:layout_width="240dp"
            android:layout_height="wrap_content" />
    </linearlayout>

    <linearlayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <checkbox
            android:id="@+id/remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="remeber password" />
    </linearlayout>

    <button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录" />

</linearlayout>

loginactivity .class

public class loginactivity extends appcompatactivity {
    private static final string tag = "loginactivity";
    private button mlogin;
    private checkbox mremember;
    private edittext musername;
    private edittext mpwd;
    private sharedpreferences msharedps;
    private sharedpreferences.editor meditor;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_login);
        initview();
    }

    private void initview() {
        msharedps = preferencemanager.getdefaultsharedpreferences(this);
        musername = findviewbyid(r.id.username);
        mpwd = findviewbyid(r.id.pwd);
        mremember = findviewbyid(r.id.remember);
        mlogin = findviewbyid(r.id.login);
        boolean isremember = msharedps.getboolean("remember_pwd", false);
        if (isremember) {
            // 将账号和密码都设置到文本框中
            musername.settext(msharedps.getstring("username", ""));
            mpwd.settext(msharedps.getstring("pwd", ""));
            mremember.setchecked(true);
        }
        mlogin.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view v) {
                string username = musername.gettext().tostring();
                string pwd = mpwd.gettext().tostring();
                // 如果账号:admin,密码:123456,就认为登录成功
                if (username.equals("admin") && pwd.equals("123456")) {
                    meditor = msharedps.edit();
                    // 检查复选框是否被选中
                    if (mremember.ischecked()) {
                        meditor.putstring("username", username);
                        meditor.putstring("pwd", pwd);
                        meditor.putboolean("remember_pwd", true);
                    } else {
                        meditor.clear();
                    }
                    meditor.apply();
                    intent intent = new intent(loginactivity.this, mainactivity.class);
                    startactivity(intent);
                    finish();
                } else {
                    log.d(tag, "用户名或密码输入错误,请重新输入");
                }
            }
        });
    }
}

运行结果,pass

Android实现文件存储案例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。