Today in a very simple Android read the code of the file is a problem encountered. To find out how shaky the Java Foundation is. The first is that the string type variable s is not assigned the initial value of the strings with the automatic connector "+ =", the result is of course an error. Then I'm going to assign s an initial value of NULL, and then connect it as a null string on the connection ... ; The code is as follows:
1 Private voidUpdateFile3 ()throwsIOException {2FileReader FileReader =NewFileReader (3"/mnt/sdcard/mytestdata/test.txt");4 Char[] buf =New Char[128];5String s =NULL;6 intstr;7 8 while(str = filereader.read (BUF))! =-1) {9s + = string.copyvalueof (buf, 0, str). toString ();Ten } One Tv.settext (s); A}
The contents of my test.txt file are "read successfully, Hey, HI". As a result, the content displayed in the TextView control TV becomes "read succeeded, hehe Hey", the workaround is only to string s = null; change to string s = ""; The specific reasons for the next time to read the Java source code to continue, this is only the first to write down their own problems, because in the internet did not find other people encounter this problem. For the first time, record your problems on the CSDN. In the future will often be here to learn from you, Exchange and share.
Connection problems in Java when the initial value of a string type variable is null