第一种:
StringBuffer sbuf = new StringBuffer(); InputStream is = new FileInputStream(fileName); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK")); String line = reader.readLine(); while (null != line) { sbuf.append(line).append("\n"); line = reader.readLine(); } // 关闭reader就行了,is.close()不必 reader.close();
第二种:
StringBuffer sbuf = new StringBuffer(); InputStream is = new FileInputStream(fileName); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK")); String line =""; while ((line = reader.readLine())!=null) { sbuf.append(line).append("\n"); } reader.close();
这两种效果是一样的。
第一种是在while之前读,读完判空,再读下一行,再进入while条件。
第二种是再while的时候读,并判断是否为空。