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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值