Java: Strings generated by substring not treated as hard coded strings
Dear all, I'm making a simple file decoder for Android 2.2 that needs to find the filename of the encoded file from a header. This filename should then be used as the filename for the decoded file (as you would expect).
The filename is identified by the substring name=
, so the actual name starts 5 places after that. The line is read by a BufferedReader and temporarily stored in currLine
.
For some reason I can't understand nor find on the web, Strings do not always seem to be Strings...
When I give the filename as
String fileOutName = "testfile.txt";
System.out.println("fileOutName contains: "+ fileOutName);
System.out.println("fileOutName type: "+ fileOutName.getClass()开发者_如何学编程);
It works like it should. However reading it from the file does not work:
String fileOutName = currLine.substring((currLine.indexOf("name=")+5));
Gives no output file, nor an IO exception. The string is parsed properly though: from the System.out debugging lines I find for both cases:
fileOutName contains: testfile.txt
fileOutName type: class java.lang.String
Anyone have any clue as to why this would not work? :S
Thanks
Ugh... Of course the substring
routine was also getting the end of line
character which obviously can't be in a filename but apparently is not returned by printline :)
So it's fixed by:
String fileOutName = currLine.substring((currLine.indexOf("name=")+5), currLine.length() -1);
Thanks for looking at this and commenting, just by posing the question the answer hit me... Can anyone please flag my answer as correct? I can't do that for the next two days it seems.
精彩评论