2015-04-03 24 views
-1

我正在寫一個需要緩存一些文件的應用程序。它需要緩存的內容是使用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"); 

    } 
+0

FX文件資源管理器沒有查看在您的應用程序的內部記憶。只有當你的設備紮根時,纔有機會。但是使用File.exists(),如果文件存在,您可以輕鬆地檢查yopurself。 – greenapps 2015-04-03 10:20:51

+0

@greenapps這就是爲什麼我把(用root)放在上面的路徑中;-) – tim687 2015-04-03 10:21:18

+0

我看到了,但它並沒有讓我覺得你的設備已經紮根了。 – greenapps 2015-04-03 10:23:23

回答

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; 
      } 
0

System.out.println("savedItemToCache");。那是錯誤的地方。你應該把它放在run()的代碼末尾。

你只創建線程,但忘了.start()他們。

+0

所有已修復的問題以及正在編寫的文件。但它只包含使用saveToCache(reader,fileName)時實際響應的第一行。 saveToCache(String,fileName)工作正常 – tim687 2015-04-03 10:29:56

+0

'但它只包含實際響應的第一行'。響應?然後在代碼中顯示您編寫的行。並顯示你如何閱讀文件。在寫作或閱讀中的錯誤是問題。你能用FX打開文件嗎?它顯示了什麼? – greenapps 2015-04-03 11:24:21

+0

如果我用fx打開代碼,它只顯示第一行:-P – tim687 2015-04-03 18:28:52

相關問題