Tôi cần một ứng dụng Mẫu thể hiện lưu tệp bộ nhớ cache trong Android và cũng làm cách nào để sử dụng phương thức getCacheDir()?
Bất cứ ai cũng có thể giúp tôi phân loại vấn đề này không? Tôi cần phải lưu tệp vào một thư mục tuyệt đối và cần phải phân tích cú pháp tệp đó.
Cảm ơn trước.Cần chương trình mẫu về "lưu tệp bộ nhớ cache" trong Android
Trả lời
Trừ khi bạn thực sự cần nó để được cache, bạn nên nhìn vào lưu trữ các tập tin liên tục hơn:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
tôi đã không cố gắng làm việc với bộ nhớ cache, nhưng có vẻ như khi bạn nhận được xử lý, nó sẽ làm việc với các lệnh tương tự được sử dụng cho các tệp liên tục.
Sử dụng (trong một hoạt động):
String textToCache = "Some text";
boolean success = GetCacheDirExample.writeAllCachedText(this, "myCacheFile.txt", textToCache);
String readText = GetCacheDirExample.readAllCachedText(this, "myCacheFile.txt");
GetCacheDirExample.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.content.Context;
public class GetCacheDirExample {
public static String readAllCachedText(Context context, String filename) {
File file = new File(context.getCacheDir(), filename);
return readAllText(file);
}
public static String readAllResourceText(Context context, int resourceId) {
InputStream inputStream = context.getResources().openRawResource(resourceId);
return readAllText(inputStream);
}
public static String readAllFileText(String file) {
try {
FileInputStream inputStream = new FileInputStream(file);
return readAllText(inputStream);
} catch(Exception ex) {
return null;
}
}
public static String readAllText(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
return readAllText(inputStream);
} catch(Exception ex) {
return null;
}
}
public static String readAllText(InputStream inputStream) {
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
public static boolean writeAllCachedText(Context context, String filename, String text) {
File file = new File(context.getCacheDir(), filename);
return writeAllText(file, text);
}
public static boolean writeAllFileText(String filename, String text) {
try {
FileOutputStream outputStream = new FileOutputStream(filename);
return writeAllText(outputStream, text);
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
public static boolean writeAllText(File file, String text) {
try {
FileOutputStream outputStream = new FileOutputStream(file);
return writeAllText(outputStream, text);
} catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
public static boolean writeAllText(OutputStream outputStream, String text) {
OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputWriter);
boolean success = false;
try {
bufferedWriter.write(text);
success = true;
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
return success;
}
}
Đẹp, nhưng viết luôn trả về false. Chỉnh sửa writeAllText để có được đúng hay sai nếu là ngoại lệ. –
Tôi đã thay đổi mã một chút để sửa lỗi đó. Tôi nghĩ rằng nên làm điều đó. – Nate
Điều này sẽ được đưa vào Tài liệu nếu chưa có tài liệu –
/** Getting Cache Directory */
File tempFile;
File cDir = getBaseContext().getCacheDir();
/* Makes a textfile in the absolute cache directory */
tempFile = new File(cDir.getPath() + "/" + "textFile.txt") ;
/* Writing into the created textfile */
FileWriter writer=null;
try {
writer = new FileWriter(tempFile);
writer.write("hello workd!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
/* Reading from the Created File */
String strLine="";
StringBuilder text = new StringBuilder();
try {
FileReader fReader = new FileReader(tempFile);
BufferedReader bReader = new BufferedReader(fReader);
while((strLine=bReader.readLine()) != null ){
text.append(strLine+"\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
- 1. Android: Xóa bộ nhớ cache lập trình
- 2. Các mẫu ActionMailer cũ được lưu trong bộ nhớ cache?
- 3. Grails \ Hibernate: Để lưu bộ nhớ cache hoặc không lưu vào bộ nhớ cache?
- 4. Cách xóa bộ nhớ cache Android
- 5. cách lưu các tệp đã tải xuống trong bộ nhớ cache android
- 6. Android - Bộ nhớ cache bitmap chiếm rất nhiều bộ nhớ
- 7. yêu cầu lưu trữ bộ nhớ cache?
- 8. Bộ nhớ cache bản vẽ Android
- 9. Android: Chiến lược bộ nhớ cache hình ảnh và bộ nhớ cache kích thước
- 10. CakePHP 2.1: Bộ nhớ cache của trình duyệt vs Xem bộ nhớ cache
- 11. Câu hỏi về bộ nhớ cache PHP vs biên dịch
- 12. Ngăn Chrome lưu trong bộ nhớ cache yêu cầu AJAX
- 13. Làm cách nào để triển khai bộ nhớ cache hình ảnh trong bộ nhớ trong Android?
- 14. Bộ nhớ cache kết quả Doctrine không lưu vào bộ nhớ cache truy vấn
- 15. Tải tệp kê khai bộ nhớ cache
- 16. Bộ nhớ cache hoặc lưu trữ trong phiên?
- 17. IE8 getJSON Dữ liệu được lưu trong bộ nhớ cache
- 18. Làm cách nào để xem bộ nhớ cache trong Android?
- 19. Android & Admob lưu trữ dữ liệu không mong muốn và giá trị bộ nhớ cache
- 20. Trình xem bộ nhớ cache DNS DNS
- 21. Bật tính năng lưu trong bộ nhớ cache của trình duyệt 3.1 trên heroku
- 22. Chọn nhiều thành phần được lưu trong bộ nhớ cache
- 23. Bộ nhớ cache đầu ra trang có được lưu trữ trong đối tượng bộ nhớ cache ASP.NET không?
- 24. Xóa bộ nhớ cache Sitecore cho một mục từ bộ nhớ cache lập trình
- 25. Mẫu Singleton trên bộ nhớ cache trong bộ nhớ liên tục
- 26. Kích thước bộ nhớ cache tối đa
- 27. Làm trống bộ nhớ cache lập trình trong iOS
- 28. Symfony2 tắt bộ nhớ cache?
- 29. Buộc trình duyệt phải đọc lại hình ảnh được lưu trong bộ nhớ cache
- 30. Cách lưu vào bộ nhớ cache xem một đoạn
Hi, Bất kỳ thông tin cập nhật liên quan đến truy vấn của tôi. Bất kỳ ai có thể giúp tôi trong việc phân loại vấn đề này? –