android 把字串內容儲存到指定路徑
---------------------------------------------------------
》儲存到外部SD卡中:
- public static void saveFile(String str) {
- String filePath = null;
- boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
- if (hasSDCard) {
- filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";
- } else
- filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "hello.txt";
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- File dir = new File(file.getParent());
- dir.mkdirs();
- file.createNewFile();
- }
- FileOutputStream outStream = new FileOutputStream(file);
- outStream.write(str.getBytes());
- outStream.close();
- } catch (Exception e) {
- e.printStackTrace();
-
}
-------------------------------------------------------------------------------
》儲存到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();
}
}
* 從檔案快取中讀取工作平臺資料
*/
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();
}
}
相關文章
- JavaScript 替換字串全部指定內容JavaScript字串
- 怎麼樣把man手冊的內容儲存到某個檔案裡面(轉)
- javascript如何替換字串中的指定內容JavaScript字串
- js替換字串中的所有指定內容JS字串
- Session儲存到指定資料庫中Session資料庫
- android下根據路徑不同拷貝檔案至指定路徑Android
- 正規表示式:後面不要包含指定的字串內容字串
- mysql 擷取指定的兩個字串之間的內容MySql字串
- js實現的替換字串中的全部指定內容JS字串
- 移動測試內容分享路徑圖
- Java 從指定URL下載檔案並儲存到指定目錄Java
- 查詢不存在指定內容的字串正規表示式字串
- 快速理解Android檔案儲存路徑Android
- 用thinkphp寫的一個例子:抓取網站的內容並且儲存到本地PHP網站
- 蘋果錄屏專家如何把影片儲存到相簿 蘋果錄屏專家影片儲存到相簿方法介紹蘋果
- Python提取文字指定內容Python
- js實現的對字串中的指定內容進行替換操作JS字串
- secureCRT螢幕日誌儲存到指定檔案的配置方法Securecrt
- MySQL 指定各分割槽路徑MySql
- postfix按指定路徑安裝
- go 把時間儲存到 MongoDB , 時間是 time 型別MongoDB型別
- mysql 如何替換資料表欄位字串中指定單詞的內容MySql字串
- Vue SpringBoot實現Html和Markdown格式內容(含圖片上傳)儲存到MySQLVueSpring BootHTMLMySql
- canvas元件繪製的內容匯出生成圖片儲存到相簿後開啟異常Canvas元件
- 一篇文章帶你使用 Python 將 txt 文件內容儲存到 excel 表中PythonExcel
- canvas clearRect()清除指定區域內容Canvas
- 利用jquery實現的向指定元素追加指定內容jQuery
- Android技能樹 — Android儲存路徑及IO操作小結Android
- Android的文字框內容改變的時候執行指定方法Android
- jquery實現的設定指定元素的文字內容和html內容jQueryHTML
- jquery獲取具有指定內容的元素jQuery
- git將指定內容寫入檔案Git
- html如何根據檔案路徑顯示檔案內容(pdf)HTML
- android 路徑大全Android
- 【usb儲存掛載】android6.0固定usb儲存掛載路徑Android
- JS把內容動態插入到DIVJS
- C#實現把指定資料夾下的所有檔案複製到指定路徑下以及修改指定檔案的字尾名C#
- Android將圖片儲存到相簿並及時看到Android