Java 向内部应用缓存目录写入内容
我正在编写一个需要缓存一些文件的应用程序。它需要缓存的内容是使用读卡器或字符串提供的。 当我使用必要的参数调用writeToCache函数时,它不会将文件写入缓存目录。我做错了什么??(使用FX浏览器,我检查了dir/SYSTEM(手机已根目录)/data/data/com.package/cache,它包含文件,但不包含具有指定文件名的文件) 顺便说一句,System.out.println(file.getPath())输出正确的路径 以下是编写读卡器对象的代码:Java 向内部应用缓存目录写入内容,java,android,file-io,Java,Android,File Io,我正在编写一个需要缓存一些文件的应用程序。它需要缓存的内容是使用读卡器或字符串提供的。 当我使用必要的参数调用writeToCache函数时,它不会将文件写入缓存目录。我做错了什么??(使用FX浏览器,我检查了dir/SYSTEM(手机已根目录)/data/data/com.package/cache,它包含文件,但不包含具有指定文件名的文件) 顺便说一句,System.out.println(file.getPath())输出正确的路径 以下是编写读卡器对象的代码: /**
/**
*
* @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");
}
System.out.println(“savedItemToCache”)代码>。那是在错误的地方。您应该将其放在run()
中的代码末尾
您只创建线程,但忘记了.start()
它们。这就是解决方案
/**
*
* @param reader The InputStreamReader 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){
System.out.println("Saving item to cache");
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 {
System.out.println("Writing file!");
OutputStream outStream;
outStream = new BufferedOutputStream(new FileOutputStream(file));
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while ((line = bufferedReader.readLine()) != null){
outStream.write(line.getBytes());
}
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
writeCache.run();
System.out.println("savedItemToCache");
}
此方法用于获取缓存文件的内容并将其放入读取器对象中
/**
*
* @param fileName the filename WITH extension
* @return The reader that you can use to parse the file
* @throws ExecutionException
* @throws InterruptedException
*/
public Reader getFromCache(String fileName) throws ExecutionException, InterruptedException {
BufferedReader reader = null;
System.out.println("getFromCache");
final File file = new File(context.getApplicationContext().getCacheDir(), fileName);
try {
FileInputStream inStream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(inStream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Reading from cache finished");
return reader;
}
FX文件资源管理器无法访问应用程序的内部内存。只有当你的设备是根有一个机会。但是使用File.exists()可以轻松地检查yopurself文件是否存在。@greenapps这就是为什么我将(带根)放在上面的路径中;-)我看到了这一点,但这并没有让我认为您的设备是根设备。Log.d(“cacheManager”,String.valueOf(reader.read())代码>。你看到这个日志声明了吗?是的,我已经更新了问题,所有的问题都解决了,并且正在编写一个文件。但在使用saveToCache(reader,fileName)时,它只包含实际响应的第一行。saveToCache(字符串、文件名)工作正常,但它只包含实际响应的第一行。回应?然后在代码中显示您编写的行。并演示如何读取文件。是写作中的错误还是阅读是问题所在。你能用FX打开这个文件吗?它显示了什么?如果我用fx打开代码,它只显示第一行:-PWhy你不告诉更多:结论是你的保存功能不正常。我已经建议你现在应该做什么了。是的,这两个saveToCache方法只写了一行。我是否需要迭代保存字符串的saveToCache中的content.getBytes()?如何迭代reader.read()方法?我正在尽我所能为您提供信息:-P谢谢!