【问题标题】:Write something to the Internal app Cache dir写一些东西到内部应用缓存目录
【发布时间】:2015-04-03 08:42:13
【问题描述】:

我正在编写一个需要缓存一些文件的应用程序。它需要缓存的内容是使用 Reader 阅读器或使用 String 提供的。 当我使用必要的参数调用 writeToCache 函数时,它不会将文件写入缓存目录。我究竟做错了什么?? (使用 FX 浏览器我检查了目录 /SYSTEM(手机已植根)/data/data/com.package/cache 它包含文件,但不包含具有指定文件名的文件)

顺便说一句,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");
            }

这是编写String对象的代码:

/**
     *
     * @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() 您可以轻松检查文件是否存在。
  • @greenapps 这就是为什么我将 (with root) 放在上面的路径中;-)
  • 我看到了,但这并没有让我认为您的设备已植根。
  • Log.d("cacheManager", String.valueOf(reader.read()));。你看到这个日志语句了吗?
  • 是的,我已经更新了问题

标签: java android file-io


【解决方案1】:

System.out.println("savedItemToCache");。那是在错误的地方。你应该把它放在run()的代码末尾。

您只创建线程但忘记.start()它们。

【讨论】:

  • 所有这些都已修复,正在写入文件。但它只包含使用 saveToCache(reader, fileName) 时实际响应的第一行。 saveToCache(String , fileName) 工作正常
  • But it only contains the first line of the actual response。回复?然后在代码中显示您编写的行。并展示你如何阅读文件。是写作错误还是阅读错误是问题。你能用FX打开文件吗?它显示了什么?
  • 如果我用 fx 打开代码,它只显示第一行:-P
  • 你为什么不说更多:结论是你的保存功能不行。我已经建议你现在应该做什么了。
  • 是的没错,这两个 saveToCache 方法都只写了一行。我是否需要遍历保存字符串的 saveToCache 中的 content.getBytes()?如何迭代 reader.read() 方法?我正在尽我所能为您提供信息:-P 谢谢!
【解决方案2】:

这就是解决办法

/**
     *
     * @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;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多