我需要一个示例应用程序,演示如何在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