Using Android RandomAccessFile from a resource

How can I access an Android resource using RandomAccessFile in Java?

Here is how I would like it to work (but it is not):

 String fileIn = resources.getResourceName(resourceID); Log.e("fileIn", fileIn); //BufferedReader buffer = new BufferedReader(new InputStreamReader(fileIn)); RandomAccessFile buffer = null; try { buffer = new RandomAccessFile(fileIn, "r"); } catch (FileNotFoundException e) { Log.e("err", ""+e); } 

Log output:

 fileIn(6062): ls3d.gold.paper:raw/wwe_obj 

The following exception appears in my console:

 11-26 15:06:35.027: ERROR/err(6062): java.io.FileNotFoundException: /ls3d.gold.paper:raw/wwe_obj (No such file or directory) 
+6
source share
3 answers

I am using this code:

 public static String readContentFromResourceFile(Context context, int resourceId) throws IOException { StringBuffer sb = new StringBuffer(); final String NEW_LINE = System.getProperty("line.separator"); InputStream is = context.getResources().openRawResource(resourceId); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String readLine = null; try { while ((readLine = br.readLine()) != null) { sb.append(readLine); sb.append(NEW_LINE); } } catch (IOException e) { throw e; } finally { br.close(); is.close(); } return sb.toString(); } 
-1
source

Like you, my situation is much simpler if I can use an instance of RandomAccessFile . The solution I finally came to is to simply copy the resource to a file in the cache, and then open this file using RandomAccessFile :

 /** * Copies raw resource to a cache file. * @return File reference to cache file. * @throws IOException */ private File createCacheFile(Context context, int resourceId, String filename) throws IOException { File cacheFile = new File(context.getCacheDir(), filename); if (cacheFile.createNewFile() == false) { cacheFile.delete(); cacheFile.createNewFile(); } // from: InputStream to: FileOutputStream. InputStream inputStream = context.getResources().openRawResource(resourceId); FileOutputStream fileOutputStream = new FileOutputStream(cacheFile); int count; byte[] buffer = new byte[1024 * 512]; while ((count = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, count); } fileOutputStream.close(); inputStream.close(); return cacheFile; } 

You should use this method:

 File cacheFile = createCacheFile(context, resourceId, "delete-me-please"); RandomAccessFile randomAccessFile = new RandomAccessFile(cacheFile, "r"); // Insert useful things that people want. randomAccessFile.close(); cacheFile.delete(); 
+2
source

Property FileNotFound. This means that you do not specify the file you want to open in String fileIn = resources.getResourceName(resourceID);

The problem is that Android can only return an InputStream raw file or FileDescriptor , but that is not enough for the RandomAccessFile constructor.

There is an open source library called Unified I / O that you can use to accomplish what you want, but I think it will just make your project β€œheavier”. Perhaps you should consider whether you can somehow avoid RandomAccessFile.

0
source

Source: https://habr.com/ru/post/902445/


All Articles