android 把字符串内容保存到指定路径

来源:互联网 发布:格格广场舞服装淘宝店 编辑:程序博客网 时间:2024/06/07 14:18
---------------------------------------------------------
》保存到外部SD卡中:
  1. public static void saveFile(String str) {  
  2.     String filePath = null;  
  3.     boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);  
  4.     if (hasSDCard) {  
  5.         filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";  
  6.     } else  
  7.         filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "hello.txt";  
  8.       
  9.     try {  
  10.         File file = new File(filePath);  
  11.         if (!file.exists()) {  
  12.             File dir = new File(file.getParent());  
  13.             dir.mkdirs();  
  14.             file.createNewFile();  
  15.         }  
  16.         FileOutputStream outStream = new FileOutputStream(file);  
  17.         outStream.write(str.getBytes());  
  18.         outStream.close();  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  

  1.     }  
-------------------------------------------------------------------------------
》保存到App内部路径:
/**
* 从缓存中读取工作平台数据
*/
public static String getWorktableBeanFromCache(Context context){
try {
File file = new File(context.getCacheDir().getAbsolutePath()+File.pathSeparator+"workPlatform");
if(!file.exists() || !file.canRead())return null;
long lastModified = file.lastModified();
if((System.currentTimeMillis() - lastModified)>Constant.FILE_CACHE_EXPIRES_HOUR*3600*1000){
file.delete();
return null;
}
FileInputStream fin = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
StringBuilder sb = new StringBuilder("");
String temp ;
while((temp=br.readLine())!=null){
sb.append(temp);
}
br.close();
fin.close();
LogManagerControl.ShowLog("tssc",sb.toString(), "i");
// sb=null;
return sb.toString();
} catch (Exception e) {

}
return null;
}
/**
* 向缓存中写入工作平台数据
*/
public static void writeWorktableBeanToCache(Context context,String json){
try {
if(TextUtils.isEmpty(json)){
return;
}

File file = new File(context.getCacheDir().getAbsolutePath()+File.pathSeparator+"workPlatform");
if(file.exists()){
file.delete();
}
if(file.createNewFile()){
FileOutputStream out = new FileOutputStream(file);
out.write(json.getBytes());
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
------------------------------------------------------------------------------------------------
》Android默认的文件保存
String FILENAME = "hello_file";    String string = "hello world!";    FileOutputStream fos;    try    {        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);        fos.write(string.getBytes());        fos.close();    }     catch (FileNotFoundException e) { e.printStackTrace(); }     catch (IOException e) { e.printStackTrace(); }    FileInputStream in = null;    try    {        in = openFileInput("hello_file.txt");        StringBuffer fileContent = new StringBuffer("");        byte[] buffer = new byte[1024];        while(in.read(buffer) != -1)        {            fileContent.append(new String(buffer));        }        finall = fileContent.toString();    }    catch (FileNotFoundException e) { e.printStackTrace(); }     catch (IOException e) { e.printStackTrace(); }
》》》》
/**
* 从文件缓存中读取工作平台数据
*/
public static String getPlatformsData(Context context) {
FileInputStream in = null;
StringBuffer fileContent;
try {
in = context.openFileInput(mFileName);
fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
in.close();
return fileContent.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return "";
}

private static String mFileName = "executive_platform_data";
/**
* 向缓存中写入工作平台数据
*/
public static void savePlatformsData(Context context, String json) {
if (StringUtils.isEmpty(json)) {
return;
}
File file = new File(context.getFilesDir(), mFileName);
if (!file.exists()) {
file.mkdir();
}
// 如果没有指定访问的模式 ,文件的模式 默认是私有的权限.
// 只有当前的应用程序可以读写这个文件 ,别的应用程序是不可以操作这个文件.
try {
// FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos;
fos = context.openFileOutput(mFileName, Context.MODE_PRIVATE);
LogManagerControl.ShowLog("FileOperatUtils", "数据被写入了!!", "V");
fos.write(json.getBytes());


fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
0 0
原创粉丝点击