内部存储
private void save(String content) {
FileOutputStream fileOutputStream=null;
try {
fileOutputStream=openFileOutput("test.txt",MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String read(){
FileInputStream fileOutputStream=null;
try {
fileOutputStream=openFileOutput("test.txt",MODE_PRIVATE);
byte[] buff=new byte[1024];
StringBuffer sb=new StringBuffer("");
int len=0;
while((len=fileOutputStream.read(buff))>0){
sb.append(new String(buff,0,len));
}
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
外部存储
private void save(String content) {
FileOutputStream fileOutputStream=null;
try {
File dir=new File(Environment.getExternalStorageDirectory(),"skypan");
if(!dir.exists()){
dir.mkdir();
}
File file=new File(dir,mFileName);
if(!file.exists()){
file.createNewFile();
}
fileOutputStream=new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String read(){
FileOutputStream fileOutputStream=null;
try {
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"skypan",mFileNmae);
fileInputStream=new fileInputStream(file);
byte[] buff=new byte[1024];
StringBuffer sb=new StringBuffer("");
int len=0;
while((len=fileOutputStream.read(buff))>0){
sb.append(new String(buff,0,len));
}
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}