我正在寫一個需要緩存一些文件的應用程序。它需要緩存的內容是使用Reader閱讀器或使用字符串提供的。 當我用必要的參數調用writeToCache函數時,它不會將文件寫入緩存目錄。我究竟做錯了什麼?? (使用FX瀏覽器我檢查了目錄/系統(電話是根源)/ data/data /com.package/cache它包含文件,但不是具有指定文件名的文件)寫一些內部應用程序緩存目錄
Btw,System.out .println(file.getPath())輸出正確的路徑!
這是寫Reader對象的代碼:
/**
*
* @param reader The reader that contains the data that is used to parse the file
* @param fileName the filename WITH extension
*/
public void saveToCache(final Reader reader, String fileName){
final File file = new File(context.getApplicationContext().getCacheDir(), fileName);
System.out.println("file.getPath() = " + file.getPath());
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(reader == null){
System.err.println("reader == null!!");
}
Thread writeCache = new Thread(){
@Override
public void run(){
try {
OutputStream outStream;
outStream = new BufferedOutputStream(new FileOutputStream(file), 8 * 1024);
outStream.write(reader.read());
outStream.flush();
outStream.close();
Log.d("cacheManager", String.valueOf(reader.read()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
System.out.println("savedItemToCache");
}
這是寫的字符串對象中的代碼:
/**
*
* @param content The content that needs to be written
* @param fileName the filename WITH extension
*/
public void saveToCache(final String content, String fileName){
final File file = new File(context.getApplicationContext().getCacheDir(), fileName);
System.out.println("file.getPath() = " + file.getPath());
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(content == null){
System.err.println("content == null");
}
Thread writeCache = new Thread(){
@Override
public void run(){
try {
OutputStream outStream;
outStream = new BufferedOutputStream(new FileOutputStream(file), 8 * 1024);
outStream.write(content.getBytes(), 0, content.getBytes().length);
outStream.flush();
outStream.close();
Log.d("cacheManager", String.valueOf(content.getBytes()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
System.out.println("savedItemToCache");
}
FX文件資源管理器沒有查看在您的應用程序的內部記憶。只有當你的設備紮根時,纔有機會。但是使用File.exists(),如果文件存在,您可以輕鬆地檢查yopurself。 – greenapps 2015-04-03 10:20:51
@greenapps這就是爲什麼我把(用root)放在上面的路徑中;-) – tim687 2015-04-03 10:21:18
我看到了,但它並沒有讓我覺得你的設備已經紮根了。 – greenapps 2015-04-03 10:23:23