字符串比较不应该相等

时间:2019-12-16 02:22:55

标签: java android string equals

我有一个函数尝试从我的应用程序管理的JSON文件中获取数据。在此功能中,我需要获取JSON文件的数据,以便可以使用GSON将其转换为对象。因此,例如,如果我有以下JSON文件:

{
    "test": [
        {"id":1,"insider":false,"name":"Name"}
    ]
}

我希望我的功能只能得到:

        {"id":1,"insider":false,"name":"Name"}

这里有功能:

private static final String open = "{\n";
private static final String start = "    \"test\": [\n";
private String data = null;
private static final String end = "    ]\n";
private static final String close = "}";

public void getFileUser() {
    Log.i("Console","getFileUser");
    FileInputStream fin;
    String receiveString;
    boolean copy = false;
    boolean finish = false;
    try {
        fin = context.openFileInput(TEST_FILE);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fin, "UTF-8"), 1024);
        StringBuilder stringBuilder = new StringBuilder();
        while ((receiveString = bufferedReader.readLine()) != null) {
            Log.i("Console", receiveString);
            // Log.i("Console", start);
            // Log.i("Console", end);
            if (receiveString.equals(start)) {
                Log.i("Console", "BEGIN");
                copy = true;
            }
            if (receiveString.equals(end)) {
                Log.i("Console", "END");
                finish = true;
            }
            if (copy && !finish) stringBuilder.append(receiveString);
        }
        fin.close();
        data = new String(String.valueOf(stringBuilder).getBytes(), Charset.forName("UTF-8"));
        Log.i("Console",data);
    }
    catch (FileNotFoundException e) { Log.i("Console", "ERROR: " + e.getMessage()); }
    catch (IOException e) { Log.i("Console", "ERROR: " + e.getMessage()); }
    catch (Exception e) { Log.i("Console", "ERROR: " + e.getMessage()); }
}

从理论上讲,我应该实现的功能应该可以实现我的目标,但是if条件永远都不能成立,因此应该包含我想要的内容的String数据为空。

我认为问题出在字符串中的新行和空格,但是我已经尝试过并且无法修复它。

由于某些用户要求,这里有存储JSON文件的函数:

private static final String open = "{\n";
private static final String start = "    \"test\": [\n";
private String data = null;
private static final String end = "    ]\n";
private static final String close = "}";

public void setFileUser(String data) {
    Log.i("Console","setFileUser");
    FileOutputStream fOut;
    try {
        fOut = context.openFileOutput(TEST_FILE, Context.MODE_PRIVATE);
        fOut.write(open.getBytes());
        fOut.write(start.getBytes());
        if (this.data != null) fOut.write(this.data.getBytes());
        String formatted_data = "        " + data + "\n";
        fOut.write(formatted_data.getBytes());
        fOut.write(end.getBytes());
        fOut.write(close.getBytes());
        fOut.close();
    }
    catch (FileNotFoundException e) { e.printStackTrace(); }
    catch (IOException e) { e.printStackTrace(); }
}

我如何获取String数据,因为可能有人会要求它:

    String data = rawResult.getText();
    Gson gson = new Gson();
    Test test = gson.fromJson(data, Test.class);
    Log.i("Console",  test.getName());
    Singleton.getInstance().setFileUser(data);
    Singleton.getInstance().getFileUser();

中间的两行都是为了确保字符串格式正确。

1 个答案:

答案 0 :(得分:0)

如果有条件,由于@ Maarten-reinstateMonica注释,我成功输入了BOTH,方法是复制String的开头和结尾,并删除行的最后一行。我必须在管理此功能的类中进行其他一些小的更改,但总体而言,我上面所说的是我所需要的。

该函数现在如下所示:

private void getFile() {
    Log.i("Console","getFile");
    FileInputStream fin;
    String receiveString;
    String data;
    boolean copy = false;
    boolean finish = false;
    try {
        fin = context.openFileInput(TEST_FILE);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fin, "UTF-8"), 1024);
        StringBuilder stringBuilder = new StringBuilder();
        while ((receiveString = bufferedReader.readLine()) != null) {
            String formatted_end = end.substring(0, end.length() - 1);
            if (receiveString.equals(formatted_end)) finish = true;
            if (copy && !finish) stringBuilder.append(receiveString);
            String formatted_start = start.substring(0, start.length() - 1);
            if (receiveString.equals(formatted_start)) copy = true;
        }
        fin.close();
        data = new String(String.valueOf(stringBuilder).getBytes(), Charset.forName("UTF-8"));
        this.loadList(data);
    }
    catch (FileNotFoundException e) { Log.i("Console", "ERROR: " + e.getMessage()); }
    catch (IOException e) { Log.i("Console", "ERROR: " + e.getMessage()); }
    catch (Exception e) { Log.i("Console", "ERROR: " + e.getMessage()); }
}