将内容写入内部应用缓存目录

时间:2015-04-03 08:42:13

标签: java android file-io

我正在编写一个需要缓存一些文件的应用。它需要缓存的内容使用Reader阅读器或使用String提供。 当我使用必要的参数调用writeToCache函数时,它不会将文件写入缓存目录。我究竟做错了什么?? (使用FX浏览器我检查了dir / 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");

    }

2 个答案:

答案 0 :(得分:0)

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

您只创建了主题,但忘了.start()他们。

答案 1 :(得分:0)

这是解决方案

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