字符串比较在应该相等时不相等
String comparison is not equal when it should be
我有一个函数试图从我的应用程序管理的 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 条件都不会变为真,所以我应该包含我想要的内容的字符串数据是空的。
我认为问题出在字符串中的新行和空格上,但我已尝试但无法修复它。
这里有存储 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 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();
中间两行是为了确保字符串的格式正确。
由于@Maarten-reinstateMonica 的评论,我成功地输入了 BOTH if 条件,方法是复制字符串的开头和结尾并删除最后一行。我不得不在管理此功能的 class 中进行一些其他小的更改,但总的来说,我上面所说的是我需要的。
现在的功能是这样的:
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()); }
}
我有一个函数试图从我的应用程序管理的 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 条件都不会变为真,所以我应该包含我想要的内容的字符串数据是空的。
我认为问题出在字符串中的新行和空格上,但我已尝试但无法修复它。
这里有存储 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 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();
中间两行是为了确保字符串的格式正确。
由于@Maarten-reinstateMonica 的评论,我成功地输入了 BOTH if 条件,方法是复制字符串的开头和结尾并删除最后一行。我不得不在管理此功能的 class 中进行一些其他小的更改,但总的来说,我上面所说的是我需要的。
现在的功能是这样的:
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()); }
}