Android存取txt

发布时间:2020-06-04 17:54:41 作者:liniuzen
来源:网络 阅读:368

/** 取SD卡路径 **/

        private String getSDPath() {

                File sdDir = null;

                boolean sdCardExist = Environment.getExternalStorageState().equals(

                                android.os.Environment.MEDIA_MOUNTED);


                // 判断sd卡是否存在

                if (sdCardExist) {

                        sdDir = Environment.getExternalStorageDirectory();

                        // 获取根目录

                }

                if (sdDir != null) {

                        return sdDir.toString();

                } else {

                        return "";

                }

        }


        /** 保存密码文件路径 **/

        private String getDirectory() {

                String dir = getSDPath() + "/" + "QQTSAVE";

                File file = new File(dir);

                if (!file.exists()) {

                        try {

                                file.mkdirs();

                        } catch (Exception e) {

                                e.printStackTrace();

                        }

                }

                return dir;

        }


        private void toSave(String user, String password) {

                File file = new File(getDirectory() + "/" + "us.txt");

                if (!file.exists()) {

                        try {

                                file.createNewFile();

                        } catch (IOException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                        }

                }


                try {

                        FileWriter fw = new FileWriter(file);

                        fw.write(user + "asd" + password);

                        fw.close();

                } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }


        }


        public String Read() {

                String sb = "";

                

                BufferedReader br;

                try {

                        br = new BufferedReader(new FileReader(getDirectory() + "/" + "us.txt"));

                        String line = "";

                        while ((line = br.readLine()) != null) {

                                sb=line;

                        }

                        br.close();

                } catch (Exception e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }

                Log.e("--------------", sb);

                return sb;

        }

        public void deleteSave(String user){

                File file = new File(getDirectory() + "/" + "us.txt");

                if (!file.exists()) {

                        try {

                                file.createNewFile();

                        } catch (IOException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                        }

                }


                try {

                        FileWriter fw = new FileWriter(file);

                        fw.write(user+"null");

                        fw.close();

                } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }

        }


推荐阅读:
  1. hibernate存取中文出现乱码
  2. python中numpy如何存取文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

equals private return

上一篇:Spring工具类PropertiesLoaderUtils的介绍

下一篇:怎么样才能学好计算机技术?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》