代码改变世界

随笔分类 -  Java

Java IO tips

2011-02-23 16:38 by RayLee, 285 阅读, 收藏,
摘要: Java IO操作最常犯的错误是忘记关闭所操作的Stream。这些Stream是跟操作系统的资源(如文件,网络)联系在一起的,而操作系统的资源是有限的,如果不能正确释放,会导致其它程序不能正常工作。 是不是所有的Stream都需要调用close()方法释放资源?与文件,网络相关的Stream应该调用close()。byte array streams可以不用。 try { OutputStream... 阅读全文

Using java.util.Scanner to process line-based strings or files

2011-02-22 14:31 by RayLee, 241 阅读, 收藏,
摘要: Scanner scanner = new Scanner(file or string); while (scanner.hasNextLine()) { processLine(scanner.nextLine()); } You can do more than that using Scanner. See details in Java API documentations. 阅读全文

关键字volatile的作用

2010-10-22 12:00 by RayLee, 456 阅读, 收藏,
摘要: 很多人可能对关键字volatile的作用不太清楚。首先,该关键字是用在多线程环境中的,今天从别人的文章中找到了一个例子说明,以帮助理解。 The volatile keyword was introduced to the language as a way around optimizing compilers. Take the following code for example: An optimizing compiler might decide that the body of the if statement would never execute, and not eve 阅读全文