我需要一個示例應用程序,演示如何在Android中保存緩存文件以及如何使用getCacheDir()方法?
任何人都可以幫我解決這個問題嗎?我需要將文件保存在絕對目錄中,並且需要解析該文件。
提前感謝。在Android中「保存緩存文件」需要示例程序
回答
,除非你真的需要它的緩存,你應該看看更持久地存儲文件:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
我沒有嘗試過與緩存的工作,但似乎一旦你的句柄,它應該與用於持久性文件的相同命令一起工作。
使用(在一個活動):
String textToCache = "Some text";
boolean success = GetCacheDirExample.writeAllCachedText(this, "myCacheFile.txt", textToCache);
String readText = GetCacheDirExample.readAllCachedText(this, "myCacheFile.txt");
GetCacheDirExample.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.content.Context;
public class GetCacheDirExample {
public static String readAllCachedText(Context context, String filename) {
File file = new File(context.getCacheDir(), filename);
return readAllText(file);
}
public static String readAllResourceText(Context context, int resourceId) {
InputStream inputStream = context.getResources().openRawResource(resourceId);
return readAllText(inputStream);
}
public static String readAllFileText(String file) {
try {
FileInputStream inputStream = new FileInputStream(file);
return readAllText(inputStream);
} catch(Exception ex) {
return null;
}
}
public static String readAllText(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
return readAllText(inputStream);
} catch(Exception ex) {
return null;
}
}
public static String readAllText(InputStream inputStream) {
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
public static boolean writeAllCachedText(Context context, String filename, String text) {
File file = new File(context.getCacheDir(), filename);
return writeAllText(file, text);
}
public static boolean writeAllFileText(String filename, String text) {
try {
FileOutputStream outputStream = new FileOutputStream(filename);
return writeAllText(outputStream, text);
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
public static boolean writeAllText(File file, String text) {
try {
FileOutputStream outputStream = new FileOutputStream(file);
return writeAllText(outputStream, text);
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
public static boolean writeAllText(OutputStream outputStream, String text) {
OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputWriter);
boolean success = false;
try {
bufferedWriter.write(text);
success = true;
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
return success;
}
}
不錯,但寫入總是返回false。如果是異常,編輯writeAllText以得到真或假。 – 2014-10-23 12:09:41
我修改了一些代碼來解決這個問題。我認爲應該這樣做。 – Nate 2014-10-23 13:31:28
如果它不存在,應將其放入文檔中 – 2016-08-10 15:45:14
/** Getting Cache Directory */
File tempFile;
File cDir = getBaseContext().getCacheDir();
/* Makes a textfile in the absolute cache directory */
tempFile = new File(cDir.getPath() + "/" + "textFile.txt") ;
/* Writing into the created textfile */
FileWriter writer=null;
try {
writer = new FileWriter(tempFile);
writer.write("hello workd!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
/* Reading from the Created File */
String strLine="";
StringBuilder text = new StringBuilder();
try {
FileReader fReader = new FileReader(tempFile);
BufferedReader bReader = new BufferedReader(fReader);
while((strLine=bReader.readLine()) != null ){
text.append(strLine+"\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
- 1. iPhone SDK遠程文件存在,不需要緩存
- 2. 如何將下載的文件保存在緩存中android
- 3. Symfony問題需要在保存實體之前保存文件
- 4. Geoserver/google_maps/Android保存緩存
- 5. 保存Silverlight源文件需要時間
- 6. 保存到程序文件文件夾的文件不存在
- 7. Yii2 redis緩存在文件緩存中
- 8. CoreDataBooks示例保存條件
- 9. 在android中保存應用程序緩存在內部sdcard的方法?
- 10. 提示要保存的文件-MVC
- 11. 如何在Android應用程序中保存Excel文件
- 12. 只需點擊一個按鈕,將文本文件保存在程序/ exe文件存在的文件夾中?
- 13. 是否需要使用清漆的php緩存例程?
- 14. 將項目保存在IIS應用程序緩存中
- 15. MVC應用程序中的不需要的緩存
- 16. ASP.NET緩存頁面。狀態已保存,但需要刷新
- 17. 文件在Android中保存在哪裏?
- 18. 在內部存儲器中保存文件android爲我的應用程序
- 19. 程序緩存
- 20. 在獨立Laravel緩存實例中添加自定義緩存驅動程序
- 21. 在Android中保留不需要的文件或功能會消耗內存
- 22. 在Android中緩存
- 23. Android應用緩存需要的存儲權限
- 24. TortoiseSVN在Windows 7中保存密碼緩存文件的位置?
- 25. Hystrix請求緩存示例
- 26. WinForms應用程序數據緩存 - 要緩存哪個層?
- 27. 需要指導:將表單實例保存到本地存儲
- 28. AlertDailogBox在android中,保存文件
- 29. 需要Android ProtoBuf RPC示例?
- 30. 將文件保存在應用程序文件夾中
你好,對我的查詢任何更新。任何人都可以幫我解決這個問題嗎? – 2010-10-27 04:54:19