我是java新手,无法理解下面示例中显示的一些基本原则.有人可以在下面的代码的上下文中解释l = inputStream.readLine())!= null的含义吗?非常感谢您的帮助.
import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.IOException; public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; try { inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new PrintWriter(new FileWriter("characteroutput.txt")); String l; while ((l = inputStream.readLine()) != null) { outputStream.println(l); } } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }
Yeldar Kurma.. 5
这段代码
while ((l = inputStream.readLine()) != null) { outputStream.println(l); }
是等价的
l = inputStream.readLine(); while (l != null) { l = inputStream.readLine(); }
因此,当读者无法读取新行时,它会返回null
,从而打破循环.
这段代码
while ((l = inputStream.readLine()) != null) { outputStream.println(l); }
是等价的
l = inputStream.readLine(); while (l != null) { l = inputStream.readLine(); }
因此,当读者无法读取新行时,它会返回null
,从而打破循环.