SharedPreferences,InternalStorage,ExternalStorage

public class BoServer {

    private Context context;

    private SharedPreferences sharedPreferences;

    public BoServer(Context context) {
        this.context = context;
    }

    public void writeIntoSharedPreferences(String content) {
        sharedPreferences = this.context.getSharedPreferences("hello", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String[] data = content.split("#");
        editor.putString("userName", data[0]);
        editor.putString("password", data[1]);
        editor.commit();
    }

    public void readFromSharedPreferences() {
        sharedPreferences = this.context.getSharedPreferences("hello", Context.MODE_PRIVATE);
        String userName = sharedPreferences.getString("userName", "null");
        String password = sharedPreferences.getString("password", "null");
        String data = userName + "#" + password;
        System.out.println(data);
    }

    public void writeIntoInternalStorage(String content) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = this.context.openFileOutput("hello.bak", Context.MODE_APPEND);
            fileOutputStream.write(content.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                    fileOutputStream = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String readFromInternalStorage() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = this.context.openFileInput("hello.bak");
            byte[] buffer=new byte[1024];
            int len=0;
            StringBuffer sb=new StringBuffer();
            while((len=fileInputStream.read(buffer))!=-1){
                sb.append(new String(buffer,0,len));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                    fileInputStream=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public boolean deleteInternalStorage(String fileName){
        return this.context.deleteFile(fileName);
    }

    public void playApi(){
        //data/data/com.example.emma.demo0428/cache
        String cacheDir=this.context.getCacheDir().getAbsolutePath();
        //data/data/com.example.emma.demo0428/files
        String fileDir=this.context.getFilesDir().getAbsolutePath();
        System.out.println("cacheDir"+cacheDir);
        System.out.println("fileDir"+fileDir);
    }
    public void writeIntoExternalStorage(String content){

       if(isSdCardExist()){
           String rootPath=Environment.getExternalStorageDirectory().getAbsolutePath();
           System.out.println(rootPath);
           File file=new File(rootPath,"hello.bak");
           if(!file.exists()){
               try {
                   file.createNewFile();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           FileOutputStream fileOutputStream=null;
           try {
               fileOutputStream=new FileOutputStream(file);
               fileOutputStream.write(content.getBytes());
           } catch (IOException e) {
               e.printStackTrace();
           } finally {
               if(fileOutputStream!=null){
                   try {
                       fileOutputStream.close();
                       fileOutputStream=null;
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               }
           }

       }
    }

    public String readFromExternalStorage() throws FileNotFoundException {

        if(isSdCardExist()){
            String path=Environment.getExternalStorageDirectory().getAbsolutePath();
            File file=new File(path,"hello.bak");
            if(file.exists()){
                StringBuffer sb=new StringBuffer();
                byte[] buffer=new byte[1024];
                int len=0;
                FileInputStream fileInputStream=new FileInputStream(file);
                try {
                    while((len=fileInputStream.read(buffer))!=-1){
                        sb.append(new String(buffer,0,len));
                    }
                    return sb.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    if(fileInputStream!=null){
                        try {
                            fileInputStream.close();
                            fileInputStream=null;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                throw new FileNotFoundException();
            }
        }
        throw new RuntimeException();


    }

    public void calculateExternalStorageCapacity(){
        if(isSdCardExist()){
            String path=Environment.getExternalStorageDirectory().getAbsolutePath();
            StatFs statFs=new StatFs(path);
            long TotalCount=statFs.getBlockCount();
            long TotalSize=statFs.getBlockSize();
            System.out.println("TotalCaoacity="+TotalCount*TotalSize/1024/1024);
        }else{
            throw new RuntimeException();
        }

    }

    public boolean isSdCardExist(){
        String state=Environment.getExternalStorageState();
        return "mounted".equals(state);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SharedPreferences是Android中用于存储和检索键值对的类。它提供了一种通用的框架,可以保存和检索原始数据类型的永久性键值对。您可以使用SharedPreferences来保存布尔值、浮点值、整型值、长整型和字符串等各种原始数据类型。这些数据将在多个用户会话中永久保存,即使应用程序已经终止也不会丢失。\[3\] 要使用SharedPreferences,您需要通过Context提供的getSharedPreferences(String name, int mode)方法获取SharedPreferences的实例。其中,第一个参数指定了SharedPreferences文件的名称(格式为xml文件),如果该文件不存在,则会创建一个新的文件。第二个参数指定了操作模式,例如MODE_PRIVATE表示只有本应用程序可以对该SharedPreferences文件进行读写,而MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE表示其他应用程序也可以读取或写入该文件。MODE_MULTI_PROCESS在Android 2.3之后已经弃用,可以省略。\[2\] SharedPreferences.Editor是用于编辑SharedPreferences的对象。它提供了一些主要的方法,例如clear()用于删除SharedPreferences中的所有数据,putXxx(String key, xxx value)用于向SharedPreferences存入指定key对应的数据,remove()用于删除SharedPreferences中指定key对应的数据项,commit()用于同步提交修改,apply()用于异步提交修改。\[1\] #### 引用[.reference_title] - *1* *2* *3* [SharedPreferences使用及原理](https://blog.csdn.net/qq_40959750/article/details/123337564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值