android 把字串內容儲存到指定路徑

desaco發表於2016-01-20
---------------------------------------------------------
》儲存到外部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();
}
}

相關文章