Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:23:18+00:00 2026-05-27T23:23:18+00:00

This is my first time on here, so I’m bit nervous and please forgive

  • 0

This is my first time on here, so I’m bit nervous and please forgive me if I don’t seem entirely clear about what I’m asking.

The problem is, Im trying to read in a file from a subfolder in the assets folder, using a method that I’ve created in a separate class. I’ve researched this for a couple of days but I’m unable to find the solution anywhere, so I’ve come here as a last resort. I needed the file reading method to be separate as there are other views/activities that will be utilising exactly the same method and I don’t think it would be wise to keep copying and pasting the same code for each activity. Ok here’s what I’ve done so far:

public class ReadAssets extends Activity {


public void read(Context context, String filepath, int textviewid) {

    try {
        InputStream input = getAssets().open(filepath);

        int size = input.available();

        // Read the entire asset into a local byte buffer.
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();

        // Convert the buffer into a string.
        String text = new String(buffer);

        // Finally insert the string into the text view.
        TextView tv = (TextView) findViewById(textviewid);
        tv.setText(text);

    } catch (IOException e) {
        // Throws an exception if an error is found
        throw new RuntimeException(e);
    }


  }
}

The activity that I would like to place this method in:

public class GeneralSetupActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gettingstarted_layout);


    ReadAssets nA = new ReadAssets();
    nA.read(this,"gettingstarted/GettingStarted.txt", R.id.displayTextView);

//  try {
//  InputStream input =getAssets().open("gettingstarted/GettingStarted.txt");
//
//  int size = input.available();
//
//  // Read the entire asset into a local byte buffer.
//  byte[] buffer = new byte[size];
//  input.read(buffer);
//  input.close();
//
// // Convert the buffer into a string.
// String text = new String(buffer);
//
//          // Finally insert the string into the text view.
//          TextView tv = (TextView) findViewById(R.id.displayTextView); 
//          tv.setText(text);
//          
//      } catch (IOException e) {
//          // Throws an exception if an error is found
//          throw new RuntimeException(e);
//      }
      }

}

I’d really appreciate it file someone could point me towards the right direction. And also I hope I’m not taking advantage but I’d like to know how I’d import and display a series of text files, one after another.

Cheers Guys,
Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:23:18+00:00Added an answer on May 27, 2026 at 11:23 pm

    If you need this available to all different types of Activity, you should consider putting the method in a superclass so all the children can use it.

    public class ExtraFunctionalActivity extends Activity
    {
        public void read(...)
        {        
            //your code
        }
    }
    
    public class GeneralSetupUtility extends ExtraFunctionalActivity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gettingstarted_layout);
    
            read(this,"gettingstarted/GettingStarted.txt", R.id.displayTextView);
    
        }
    }
    

    Otherwise, if this method is needed by a bunch of unrelated classes, you could put it in a utility class;

    public class FileUtil
    {
    
        public static void read(...)
        {
            //your code
        }
    
    }
    

    then you can call it where needed with

    FileUtil.read(args here);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this's my first time to ask a question here. So, I don't have
Its my first time here and I don't know how to indent this sorry
this is my first time asking a question here so apologies if I am
This is my first time posting here, so please be kind ;-) EDIT My
This is my first time here so I hope I post this question at
this is my first time posting here, I have a question which I have
Here's the scenario: if this is a user's first time logging into my web
This is my first time using joomla. I don't know if I'm using the
this is my first time here (I find the website very useful). I'm new
this is my first time posting on here and have read alot of helpful

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.