文件存在测试不适用于缓存文件夹...?

发布于 2024-10-05 06:36:37 字数 1401 浏览 8 评论 0原文

我的程序检查默认缓存文件夹中是否存在特定文件。如果文件存在,它将打开它并读取内容。如果该文件不存在,则会从网络中提取该文件并将其存储在缓存文件夹中。我遇到的问题是,无论文件是否在缓存文件夹中,我的文件测试总是返回 false。有趣的是,即使文件测试返回 false,我仍然可以从缓存文件夹中打开文件并读取它。我可以提取缓存文件夹中的文件列表,并且可以看到该文件在那里,但是当我执行文件测试以查看该文件是否在那里时,它返回 false,即使我知道该文件在那里并且我可以打开它并查看其内容。

我尝试了常规的 contains() 测试,甚至一一读取缓存目录中的每个文件,并将名称与我正在查找的文件进行比较,但仍然返回 false。

感谢您提前提供任何帮助!

String file = "test.txt"
String content = "testing";

 putFile(file, content);
 Boolean fileIsThere = checkFile(file);

 public Boolean checkFile(String file){
  Boolean fileExists = false;

  // regular file test - always returns false, even if the file is there
  File f = new File(file);
  if (f.exists())
   fileExists = true;

  // comparing each individual file in the directory - also returns false
  String[] dirFiles = fileList();
  for (int i = 0; i < dirFiles.length; i++) {
   if (dirFiles[i] == file){
    fileExists = true;
    break;
   }
  }

  return fileExists;
 }


 public void putFile(String file, String content){
  try {
   FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE);
   fos.write(content.getBytes());
   fos.close();
  } catch (Exception e) {
   Log.w("putFile", "Error (" + e.toString() + ") with: " + file);
  }
 }

有什么想法吗?我在想,由于我将文件放入缓存文件夹中,因此我在文件测试中总是会得到错误的结果。我只是想看看是否有其他人遇到过这个问题并解决了它,或者我是否必须创建一个特定的目录并将我的文件存储在那里,或者其他什么。 putFile() 中的“Context.MODE_PRIVATE”与它有什么关系吗?

My program checks if a specific file exists in the default cache folder. If the file exists, it opens it and reads the contents. If the file does not exist, the file gets pulled from the web and is stored in the cache folder. The problem I'm having is that, no matter if the file is in the cache folder or not, my file test always returns false. The funny thing is that, even though the file test returns false, I can still open the file from the cache folder and read it. I can pull a list of files in the cache folder and I can see the file is there, but when I do the file test to see if the file is there, it returns false, even though I know the file is there and I can open it and see it's contents.

I tried the regular exists() test and even reading each file in the cache directory one by one and comparing the name to the file I'm looking for and still returns false.

Thanks for any help in advance!

String file = "test.txt"
String content = "testing";

 putFile(file, content);
 Boolean fileIsThere = checkFile(file);

 public Boolean checkFile(String file){
  Boolean fileExists = false;

  // regular file test - always returns false, even if the file is there
  File f = new File(file);
  if (f.exists())
   fileExists = true;

  // comparing each individual file in the directory - also returns false
  String[] dirFiles = fileList();
  for (int i = 0; i < dirFiles.length; i++) {
   if (dirFiles[i] == file){
    fileExists = true;
    break;
   }
  }

  return fileExists;
 }


 public void putFile(String file, String content){
  try {
   FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE);
   fos.write(content.getBytes());
   fos.close();
  } catch (Exception e) {
   Log.w("putFile", "Error (" + e.toString() + ") with: " + file);
  }
 }

Any ideas? I'm thinking that since I'm putting the files in the cache folder, I will always get false on the file test. I just want to see if anyone else came across this and has a fix for it, or if I have to make a specific directory and store my files there, or something else. Could "Context.MODE_PRIVATE" in putFile() have anything to do with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-10-12 06:36:37

如果你想测试存储在上下文存储中的文件的存在data/data/nameOfPackage/files/text.txt你必须像这样重写字符串文件

String file = "/data/data/nameOfPackage/files/test.txt"

然后你可以检查测试的exists()方法.txt 文件。我希望它能帮助你。 :)

If you want to test existention of file stored in your Context storage data/data/nameOfPackage/files/text.txt you have to rewrite String file like this

String file = "/data/data/nameOfPackage/files/test.txt"

Then you can check exists() method of your test.txt file. I hope it will help you. :)

森林散布 2024-10-12 06:36:37

if (f.exists() && f.length() > 0) fileExists = true;

这对我有用!

if (f.exists() && f.length() > 0) fileExists = true;

This works for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文