Android, strings loaded from .txt file are not being escaped
I am loading data from a resource within my own application, and the escape characters I place are not being processed the way I expect them to be. For example, a line in my resource would look like this:
Ellington Human Sciences Building<>EHS<>Human Performance Sciences Building\nNeighbor to Ellington Human Sciences Annex (EHSA)<>292<>482<>73<>25<>Human Sciences
Ellington Human Sciences Annex<>EHSA<>Human Performance Sciences Building\nNeighbor to Ellington Human Sciences Building (EHS)<>340<>464<>28<>20<>Human Sciences
my file reader looks like so:
private synchronized void loadPOIs(Resources resource) throws IOException {
if (mLoaded) return;
InputStream inputStream = resource.openRawResource(R.raw.pois);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "<>");
if (strings.length < 7) continue;
POI poi = addPOI(strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6]);
if (strings.length == 8) {
final int len = strings[7].length();
for (int i = 0; i < len; i++) {
开发者_如何学运维 final String prefix = strings[7].substring(0, len - i);
addMatch(prefix, poi);
}
}
}
} finally {
reader.close();
}
mLoaded = true;
}
strings[2] would be the line holding the information about the Point of Interest, and they contain the "\n" character. When I call poi.getInfo() (the getter method of retrieving the info, returns a String) the output allows the "\n" to persist.
any ideas?
You are reading text from a file, and '\n' is just as valid text as any other and does not have any special connotation within a text file. If you want a newline instead, then write a newline in your txt file. It's sure easier than performing scaping over text, and you control the source of the data so there should be no trouble in modifying it.
精彩评论