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);
}
}
SharedPreferences,InternalStorage,ExternalStorage
最新推荐文章于 2017-11-23 16:53:00 发布