D
D
Dmitry Laptev2015-04-16 18:07:15
Android
Dmitry Laptev, 2015-04-16 18:07:15

How to search within a file?

Actually, the task arose - to put a text file into the /assets folder, do a search on it and return the result. But IO Exception persists, I am attaching a piece of code that directly does this.

String str = null;


        try {




            LineNumberReader lnr = new LineNumberReader(new BufferedReader(
                   new InputStreamReader(getAssets().open("elements.txt"))));
                   
            String inp = comp.getText().toString();

            Pattern p1 = Pattern.compile(inp + " = (\\w+).*"); 
            while (((str = lnr.readLine()) != null)) {
                Matcher m = p1.matcher(str);
                if (m.find()) {
                    fin = m.group(1);
                    res.setText(fin);
                    //res.setText(m.group(1));
                }
                lnr.close();
            }
        } catch (IOException iо) {
            res.setText("Error!");
        }
    }
A huge request, kind people, if my code is completely dead, write a working version, I will be very grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2015-04-17
@i_am_mrld

it is not clear from the code where the problem could be, apparently the Exception comes from the file system, for which reason it can only be determined by the Message and Trace of this Exception, which you did not write. I think you need to rewrite a piece

catch (IOException io) { res. setTex("Error!");
so that it prints an error rather than a meaningful "Error!"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question