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

  • SEARCH
  • Home
  • 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 7582061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:49+00:00 2026-05-30T18:20:49+00:00

public class Utils { public static List<Message> getMessages() { //File file = new File(file:///android_asset/helloworld.txt);

  • 0
public class Utils {
    public static List<Message> getMessages() {
        //File file = new File("file:///android_asset/helloworld.txt");
        AssetManager assetManager = getAssets();
        InputStream ims = assetManager.open("helloworld.txt");    
     }
}

I am using this code trying to read a file from assets. I tried two ways to do this. First, when use File I received FileNotFoundException, when using AssetManager getAssets() method isn’t recognized.
Is there any solution here?

  • 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-30T18:20:50+00:00Added an answer on May 30, 2026 at 6:20 pm

    Here is what I do in an activity for buffered reading extend/modify to match your needs

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(
            new InputStreamReader(getAssets().open("filename.txt")));
    
        // do reading, usually loop until end of file reading  
        String mLine;
        while ((mLine = reader.readLine()) != null) {
           //process line
           ...
        }
    } catch (IOException e) {
        //log the exception
    } finally {
        if (reader != null) {
             try {
                 reader.close();
             } catch (IOException e) {
                 //log the exception
             }
        }
    }
    

    EDIT : My answer is perhaps useless if your question is on how to do it outside of an activity. If your question is simply how to read a file from asset then the answer is above.

    UPDATE :

    To open a file specifying the type simply add the type in the InputStreamReader call as follow.

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(
            new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 
    
        // do reading, usually loop until end of file reading 
        String mLine;
        while ((mLine = reader.readLine()) != null) {
           //process line
           ...
        }
    } catch (IOException e) {
        //log the exception
    } finally {
        if (reader != null) {
             try {
                 reader.close();
             } catch (IOException e) {
                 //log the exception
             }
        }
    }
    

    EDIT

    As @Stan says in the comment, the code I am giving is not summing up lines. mLine is replaced every pass. That’s why I wrote //process line. I assume the file contains some sort of data (i.e a contact list) and each line should be processed separately.

    In case you simply want to load the file without any kind of processing you will have to sum up mLine at each pass using StringBuilder() and appending each pass.

    ANOTHER EDIT

    According to the comment of @Vincent I added the finally block.

    Also note that in Java 7 and upper you can use try-with-resources to use the AutoCloseable and Closeable features of recent Java.

    CONTEXT

    In a comment @LunarWatcher points out that getAssets() is a class in context. So, if you call it outside of an activity you need to refer to it and pass the context instance to the activity.

    ContextInstance.getAssets();
    

    This is explained in the answer of @Maneesh. So if this is useful to you upvote his answer because that’s him who pointed that out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class CollectionsFilter { public static void main(String[] args) { List<Integer> list = Arrays.asList(new
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
public class Main { public static void main(String args[]) { List list = new
I have created a public static class utils.cs I want to use it in
I wrote two methods in class Util: public static final <T> T[] copy1(T[] source)
Why doesn't this class compile? import java.util.*; public class Caesar { public static void
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
I have a simple Java class that has some methods: public class Utils {

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.