Return to Snippet

Revision: 44074
at April 5, 2011 22:33 by BenClayton


Initial Code
static public String readStringFromResource(Context ctx, int resourceID) {
	    StringBuilder contents = new StringBuilder();
	    String sep = System.getProperty("line.separator");
	    
	    try {			
	      InputStream is = ctx.getResources().openRawResource(R.raw.trails);

	      BufferedReader input =  new BufferedReader(new InputStreamReader(is), 1024*8);
	      try {
	        String line = null; 
	        while (( line = input.readLine()) != null){
	          contents.append(line);
	          contents.append(sep);
	        }
	      }
	      finally {
	        input.close();
	      }
	    }
	    catch (FileNotFoundException ex) {
	    	Log.e(TAG, "Couldn't find the file " + resourceID  + " " + ex);
	    	return null;
	    }
	    catch (IOException ex){
	    	Log.e(TAG, "Error reading file " + resourceID + " " + ex);
	    	return null;
	    }
	    
	    return contents.toString();
	  }

Initial URL

                                

Initial Description
put your text files into the res/raw folder.

Initial Title
Android: Read a text file from resources

Initial Tags

                                

Initial Language
Java