Posted by
pat on October 24, 2004 at 1:18 AM PDT
One of the things I've always wanted in Java is a "one liner" trick to read all of the text from a stream. For example, I often want to be able to grab the contents of a URL or file as a simple String, without a lot of typing. The URL class tantalizingly holds out its getContent() method, but sadly, content handlers were never really taken seriously. I don't even particularly care about performance, I'd just like something for the simple case, in standard Java, that's not too hard to remember. Well, the Java 1.5 java.util.Scanner class finally has the answer...
Suppose I have a stream:
InputStream source = new URL("http://pat.net/misc/foo.txt").openStream();
The canonical way to gather it to a String has always been to use a BufferedReader, e.g.
BufferedReader br = new BufferedReader( new InputStreamReader( source ) );
StringBuffer text = new StringBuffer();
for ( String line; (line = br.readLine()) ! = null )
text.append( line );
This is about 4 lines of tediousness code (assuming the resulting StringBuffer is good enough), uses two classes, a loop, and too many parentheses. I must have typed code like this a million times, as I bet a lot of people have.
I've often been tempted to try to shorten it a bit using the DataInputStream readFully() method:
byte [] buf = new byte[ source.available() ];
new DataInputStream( source ).readFully( buf );
String text = new String( buf );
That would be a bit less typing and involve only an array and a class. The problem is that it relies on the input stream's available() method to reflect the total size of the data to be returned... which in general it doesn't. The available() method works for files and you could always substitute your own size if you can get it from other meta-data, but it's still a messy solution and doesn't exactly roll off of the finger tips.
Finally now with Java 1.5's Scanner I have a true one-liner:
String text = new Scanner( source ).useDelimiter("\\A").next();
One line, one class. The only tricky is to remember the regex \A, which matches the beginning of input. This effectively tells Scanner to tokenize the entire stream, from beginning to (illogical) next beginning. As a bonus, Scanner can work not only with an InputStream as the source, but also a File, Channel, or anything that implements the new java.lang.Readable interface. For example, to read a file:
String text = new Scanner( new File("poem.txt") ).useDelimiter("\\A").next();
Finally, before someone chastizes me I should point out that you can accommodate a specific character set with all of the above examples. In the first you'd set the charset in the InputStreamReader, in the second you'd specify it with the String constructor, and in the Scanner example you can pass a charset to the constructor.
Enjoy!
相关推荐
8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.javaScanner类.zip8.java...
36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入Scanner.zip36.java数据输入...
android BarcodeScanner源码.zip
sonar-scanner-4.0.0.1744-windowssonar-scanner-4.0.0.1744-windowssonar-scanner-4.0.0.1744-windowssonar-scanner-4.0.0.1744-windows
web vulnerability scanner 10.x经典的网站漏洞测试工具
可以方便的监控活动IP地址,对网段IP地址进行全面扫描
JAVA Scanner输入.pdf
Acunetix Web Vulnerability Scanner8.x Enterprise Edition KeyGen 使用说明:先通过官方地址下载最新的企业版安装包[注册机只支持企业版,不支持free版],安装完成后关闭程序打开破解补丁,点Patch后可正常注册了,...
BarcodeScanner4.2.apk )
Acunetix Web Vulnerability Scanner 10.x Consultant Edition KeyGen By Hmily[LCG] 神器应用扫描
一、什么是 Java Scanner 类? Java Scanner 类是 Java 中一个用于读取用户输入的类,它可以从标准输入、文件、字符串等多种输入源中读取数据,并将数据转换为 Java 中的各种数据类型。 二、引用数据类型 我们要学的...
包含 Acunetix Web Vulnerability Scanner 11.0.170951158、Crack、汉化补丁。
【一站式服务】 1.BarcodeScanner4.31.apk 2.android-integration.jar 3.core.jar 4.实用上手文档 5.实测可用例子 精心整理,超值、实用…… 附:在线帮助文档:http://zxing.org/w/docs/javadoc/index.html
HBase数据读取流程解析-2——构建scanner体系.pdf 学习资料 复习资料 教学资源
com.huawei.scanner.2206091545.apk.1
Windows.Devices.Scanners
sonar-scanner-3.1.0.1141-windows,用于检测代码质量
Java中Scanner类的用法,java初学者必看
WebCruiser.Web.Vulnerability.Scanner.Enterprise.v2.5.0.Cracked 找了半天才找到