Frage

Can someone help me in this code. The if statement isn't run but the term is true.. (I see it n the Log) So what is the problem with this code? There are two line in the .txt file so the while is running twice.

The code:

String tag = null;
    String path = "/data/data/com.barnabas.vac.ballog/files";
    try {

        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(path+"/Settings.txt")));
        String line = "";
        Log.e(tag, "A");
        while((line = (br.readLine())) != null) {

            if(line.getBytes() == "RunApp: ON".getBytes())
            {
                RunApp.setChecked(true);
                Log.e(tag, "B => " + line);
            }
            else if(line.getBytes() == "Battery: ON".getBytes())
            {
                Battery.setChecked(true);
                Log.e(tag, "C => " + line);
            }
            Log.e(tag, line.getBytes() + " => " + "RunApp: ON".getBytes());
            Log.e(tag, "D => " + line);
        }
        Log.e(tag, "E");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And the Log:

03-26 18:55:05.405: E/(25690): A
03-26 18:55:05.405: E/(25690): [B@419d1aa8 => [B@419d1bf8
03-26 18:55:05.405: E/(25690): D => RunApp: ON
03-26 18:55:05.405: E/(25690): [B@419d1f10 => [B@419d1fe8
03-26 18:55:05.405: E/(25690): D => Battery: ON
03-26 18:55:05.405: E/(25690): E
War es hilfreich?

Lösung

You're comparing byte[] instances, arrays of bytes. The == operator won't compare array contents, only to see if they're the same array object, which they aren't.

To compare array contents, use Arrays.equals:

if(Arrays.equals(line.getBytes(), "RunApp: ON".getBytes()))

You'll need to change your other array comparisons similarly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top