Suppose the file has two lines.
First line
Second line
Then read the loop twice, the first time s is assigned to readContent, and the value of readContent is "the first line", then loop, read the second line, and assign the value again, then readContent will become "the second line", and the value of the first line will be lost;
The function of StringBuff here is to connect the read contents into a string;
However, there is something wrong with the example here, but the problem is not on the StringBuffer. On the S variable, the middle two steps of S are unnecessary, and the loop is directly changed to the following form:
Delete s.getBytes () and new String.
In addition, in addition to the above problems, let's summarize the problems of this code:
1, file resources are not released.
2. In the case that multithreading is not involved, StringBuilder class should be used for string splicing instead of StringBuffer, because StringBuilder is faster (but StringBuilder thread is unsafe and StringBuffer thread is safe);
3. There is a problem with exception handling. If there is an IO exception, return the string of exception information? So how do you distinguish whether this string is abnormal or what you read from the file? Instead of returning a string, you should directly declare an exception, and throw it out until the top program that can handle it can handle it.
Based on the above points, modify the code as follows:
Pay attention to line 27, stating that this method may throw an IOException exception, which will be handled by the main program later. In addition, pay attention to line 30. When creating InputStreamReader, you specified the file encoding as utf8 to avoid garbled. If your file stores other encoding types, garbled will appear.
The calling code is as follows:
The getContent function calls the main program.
Note that lines 18 and 2 1 deal with normal and abnormal situations respectively. When reading an exception, line 2 1 will be called to print the exception information.
The overall code is as follows:
All codes
Next, let's look at the code execution.
1, when the file we want to read does not exist, the execution result is as follows:
When the file we want to read does not exist, an exception is thrown.
Look, when the file doesn't exist, an exception is thrown, which is captured and printed by the exception handler. You can see which line of code is wrong [getContent(App.java:29)], which means that the getContent method in App.java file is wrong, and the 29 lines of the file are wrong.
2. What about under normal circumstances? Of course, the contents of the output file, we put a file with the following contents in "D:\\file.txt":
Content of the file to be read
The implementation results are as follows:
Under normal circumstances, output the contents of the file.
Finally, I don't know what book you are reading, whether it is a textbook or not, but I feel that the quality is not high. Here are some recommended books for you to buy and have a look:
1、Head First java
2, rookie tutorial Java
3, the most authoritative, naturally The Java? Language Specificati
I wish you a smooth road to study.