How to read a txt file store the value in the whole type?

Asked

Viewed 89 times

0

I’m using this example:

try {
    AssetManager assetManager = getResources().getAssets();
    InputStream inputStream = assetManager.open("nome-do-arquivo.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String linha;

    while((linha = bufferedReader.readLine())!=null){
      nome = linha;
       id = bufferedReader.readLine() // Aqui é um tipo inteiro
    }
    inputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

In the arquivo txt is like this

fulano
R.drawble.image

I would like how to do R.drawble.image store in a whole type because I tried to convert String for int but it didn’t work out.

  • You want to store a text in a variable int?

  • @jbueno is not that, it is time to read write to an integer type variable int

  • @jbueno R.drawable.image he’s the whole kind

2 answers

3


You can try something like this on your while:

while ((linha = bufferedReader.readLine()) != null) {
    if (linha.startsWith("R.drawble.")) {
        String name = linha.replace("R.drawble.", "");
        int resource = getResources().getIdentifier(name, "drawable", getPackageName());
    }
}

Where resource shall be the whole corresponding to drawable of your app.

  • I didn’t understand could comment on the code to see if it gives a light

  • What you want, is that from the line you have in your txt, you get the integer that corresponds to a drawable from your app, it’s not?

  • Yes that’s what I’m doubting in this part String name = linha.replace("R.drawble.", "");

  • It is because by the method getIdentifier(), the first parameter is the name of the drawable, then you need to delete "R.drawable." from your line and use only the part that matters, which is the name itself.

  • I get it, I’m gonna test

1

As @Paulo Rodrigues said, you can use the method getIdentifier resource.

Follows the documentation

Example of use:

   try {
        AssetManager assetManager = getResources().getAssets();
        InputStream inputStream = assetManager.open("nome-do-arquivo.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String linha;


        while((linha = bufferedReader.readLine())!=null){
            String id = bufferedReader.readLine();
            if(id.contains(".")){ // garantimos que haja pontos na string...
                String[] valores = id.split("."); // quebramos a string nos pontos {"R", "rawble", image"}
                // Vamos pegar sua identificação conforme os valores...
                int resource = getResources().getIdentifier(valores[2], valores[1], getActivity().getPackageName());
            }


        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.