-1

This is the second thread that I have started for the same question since I did not get a right answer/working answer for my earlier question. I am trying to read a text file from my assests folder. The code works on the desktop but does not on Android. I understand I need a context or an asset manager but then I cannot get resolve method. I have tried everything so far, but I am unable to find a tutorial that works. I even changed the java version to 1.7 to get the try with resources. I am still a noob, so any help would be great. Here is the full class of reading the file and generating a word.

public class WordGenerator {

public String randomWord;
public Random rand;
public char [] randomWordChar;


public ArrayList<String> words = new ArrayList<String>();

private Context mCtx; //<-- declare a Context reference

public void WordGenerator() {


    rand = new Random();

    String all = new String("words.txt");
    String line;


    try {

        BufferedReader br = new BufferedReader(new FileReader(("words.txt")));
        if (!br.ready()) {
            throw new IOException();
        }
        while ((line = br.readLine()) != null) {
            words.add(line);
        }
        br.close();
    } catch (IOException e) {
        System.out.println(e);
    }
        int size = words.size();
        Random rn = new Random();
        int randWord = rn.nextInt(size);
        randomWord = words.get(randWord);
        randomWordChar = randomWord.toCharArray();
    }

}

I even had a couple of tries of separating the load process into a different void class but failed. If possible help thank you. Here is the link to the other question so u know the answers that did not work!!

Reading a plain text file

Community
  • 1
  • 1
Joe
  • 65
  • 1
  • 8
  • Have you tried doing something like this **br = new BufferedReader( new InputStreamReader(getAssets().open("words.txt")));** ?? – sha May 16 '16 at 00:51
  • yes but for some reason i cant get the getAssests to work.. it always returns an error cannot resolve symbol.. so does context.getAsssests assetmanager.getAssests same result – Joe May 16 '16 at 01:04
  • Can you please just post the package of the Context you are using? – sha May 16 '16 at 01:24
  • import javax.naming.Context; – Joe May 16 '16 at 01:26
  • Yes.. Is your package of Context is **android.content.Context**? I cannot see any other reason for not resolving the **getAssets()** method unless using Context from any other thirdparty packages. – sha May 16 '16 at 01:30
  • This answers your issue. You should use **android.content.Context** to get the "getAssets()" method working – sha May 16 '16 at 01:31
  • thank you .. quick question do i move the whole project to the android src package or just this class?? can i call on a class from the android package to the core package?? also please post and answer so i can mark it.. – Joe May 16 '16 at 01:36
  • I could not get your question properly. I suppose your classfiles and resources are residing under src folder of Android project. – sha May 16 '16 at 01:39
  • no my whole project is under core/src but i have a desktop and android.. i am using libgdx.. will try to move only this class – Joe May 16 '16 at 02:08

1 Answers1

0

You should use android.content.Context to get the "getAssets()" method working.

Observed you are using javax.naming.Context for the context.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
sha
  • 1,410
  • 2
  • 18
  • 37
  • also would be a great help if u know the code to use now that i can use getAsset... thank you – Joe May 16 '16 at 02:10
  • Check this... http://stackoverflow.com/questions/9544737/read-file-from-assets. This might help u – sha May 16 '16 at 02:11