स्मृति में बीच में सब कुछ बफर किए बिना इनपुट स्ट्रीम में आगे बढ़ना और वापस जाना संभव नहीं है। यह बेहद महंगा हो सकता है, और कुछ मनमानी आकार की एक (बाइनरी) फ़ाइल पढ़ने के लिए एक स्केलेबल समाधान नहीं है।
आप सही हैं: आदर्श रूप से, कोई RandomAccessFile
का उपयोग करेगा, लेकिन संसाधनों से पढ़ने के बजाय इनपुट स्ट्रीम प्रदान करता है। ऊपर दी गई टिप्पणियों में उल्लिखित सुझाव एसडी कार्ड में फ़ाइल लिखने के लिए इनपुट स्ट्रीम का उपयोग करना है, और वहां से फ़ाइल को यादृच्छिक रूप से एक्सेस करना है। तुम्हें पता है, एक अस्थायी निर्देशिका में फाइल को लिख इसे पढ़ने के लिए, और उपयोग के बाद हटाने पर विचार कर सकता है:
String file = "your_binary_file.bin";
AssetFileDescriptor afd = null;
FileInputStream fis = null;
File tmpFile = null;
RandomAccessFile raf = null;
try {
afd = context.getAssets().openFd(file);
long len = afd.getLength();
fis = afd.createInputStream();
// We'll create a file in the application's cache directory
File dir = context.getCacheDir();
dir.mkdirs();
tmpFile = new File(dir, file);
if (tmpFile.exists()) {
// Delete the temporary file if it already exists
tmpFile.delete();
}
FileOutputStream fos = null;
try {
// Write the asset file to the temporary location
fos = new FileOutputStream(tmpFile);
byte[] buffer = new byte[1024];
int bufferLen;
while ((bufferLen = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bufferLen);
}
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {}
}
}
// Read the newly created file
raf = new RandomAccessFile(tmpFile, "r");
// Read your file here
} catch (IOException e) {
Log.e(TAG, "Failed reading asset", e);
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {}
}
if (afd != null) {
try {
afd.close();
} catch (IOException e) {}
}
// Clean up
if (tmpFile != null) {
tmpFile.delete();
}
}
स्रोत
2013-07-05 18:22:07
मुझे ऐसा नहीं लगता है, क्योंकि है कि संसाधन फ़ाइल अभी भी संकुचित APK में संग्रहीत किया जाएगा, इसलिए InputStream आप हो रही है शायद एक ZipEntryInputStream है। और चूंकि यह संपीड़ित है, आपको इसे स्ट्रीम के रूप में पढ़ना होगा। फ़ाइल में अलग-अलग पदों की तलाश करना शायद ऐसा कुछ नहीं है जो RandomAccessFile सक्षम है। – Brigham
"यादृच्छिक अभिगम फ़ाइल" ... और आप कहते हैं कि मांग संभव नहीं है? – fabspro
आपको फ़ाइल को एसडी कार्ड में कॉपी करने की आवश्यकता है (http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard देखें), फिर आप उस पर RandomAccessFile का उपयोग कर सकते हैं । – ron