Reads a file from /raw/res/ and returns it as a String : File « File « Android






Reads a file from /raw/res/ and returns it as a String

    
//package com.lexandera.mosembro.util;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import android.content.res.Resources;

/**
 * Utility functions for reading files
 */
public class Reader
{
    /**
     * Reads a file from /raw/res/ and returns it as a String
     * @param res Resources instance for Mosembro
     * @param resourceId ID of resource (ex: R.raw.resource_name)
     */
    public static String readRawString(Resources res, int resourceId)
    {
      StringBuilder sb = new StringBuilder();
        Scanner s = new Scanner(res.openRawResource(resourceId));

        while (s.hasNextLine()) {
          sb.append(s.nextLine() + "\n");
        }
        
        return sb.toString();
    }
    
    /**
     * Reads a file from /raw/res/ and returns it as a byte array
     * @param res Resources instance for Mosembro
     * @param resourceId ID of resource (ex: R.raw.resource_name)
     * @return byte[] if successful, null otherwise
     */
    public static byte[] readRawByteArray(Resources res, int resourceId)
    {
        InputStream is = null;
        byte[] raw = new byte[] {};
        try {
            is = res.openRawResource(resourceId);
            raw = new byte[is.available()];
            is.read(raw);
        }
        catch (IOException e) {
            e.printStackTrace();
            raw = null;
        }
        finally {
            try {
                is.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return raw;
    }
    
    /**
     * Reads a remote file and returns it as as a String
     * @param fileUrl URL of remote file
     */
    public static String readRemoteString(String fileUrl)
    {
        StringBuilder sb = new StringBuilder();
        
        try {
            URLConnection connection = (new URL(fileUrl)).openConnection();
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            connection.connect();
            
            Scanner s = new Scanner(connection.getInputStream());
            
            while (s.hasNextLine()) {
                sb.append(s.nextLine() + "\n");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
        return sb.toString();
    }
    
    /**
     * Reads a remote file and returns it as as a byte array
     * @param fileUrl URL of remote file
     * @return byte[] if successful, null otherwise
     */
    public static byte[] readRemoteByteArray(String fileUrl)
    {
        InputStream is = null;
        byte[] raw = new byte[] {};
        try {
            URLConnection connection = (new URL(fileUrl)).openConnection();
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            connection.connect();
            is = connection.getInputStream();
            raw = new byte[is.available()];
            is.read(raw);
        }
        catch (Exception e) {
            e.printStackTrace();
            raw = null;
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return raw; 
    }
}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.File Name Extension Utils
15.Write and append string to file
16.Transfers required files to the the private file system part in order to be access them from C Code.
17.returns a canonical line of a obj or mtl file.
18.Trims down obj files, so that they may be parsed faster later on.
19.Format File Size:KB, MB, GB
20.Get File Contents as String
21.Read File and return CharSequence
22.Copy File
23.Read/write From FileSystem, browse Account
24.Create Path and file under External Storage
25.Get File Extension Name
26.Extract File Name From String
27.Write String to file
28.Create an output file from raw resources.
29.Copies a directory from a jar file to an external directory.
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.