NNTP-Posting-Date: Fri, 11 Feb 2011 03:01:08 -0600 Date: Fri, 11 Feb 2011 17:01:08 +0800 From: Peter Duniho User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Shortest way to read all lines (one by one) from a text file? References: <4d54f158$0$6975$9b4e6d93@newsspool4.arcor-online.net> In-Reply-To: <4d54f158$0$6975$9b4e6d93@newsspool4.arcor-online.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 16 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 219.74.123.211 X-Trace: sv3-Tj4aaXAPgv7g2koaOfOOS/mqZ0E1VpNRvLoyFu1293zI8LnG6qnL5UeTXB4D+tEa+v8g+I151s7724O!+FGiGhm5it2IjNFZ+1PQJh7dqj+cy6JnegYN2/wKbSMUeXMVeNDpZjte1l3Isy/9f8mX+5G3i2Zp!cSpj3F+aVGn9zQ6JXLfVJ18FYYwatq8tSB3GKYX7kMSE X-Complaints-To: abuse@iinet.com X-DMCA-Complaints-To: abuse@iinet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1902 Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!feed.ac-versailles.fr!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.246.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!216.196.98.144.MISMATCH!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail Xref: csiph.com comp.lang.java.programmer:26047 On 2/11/11 4:20 PM, Robin Wenger wrote: > Ok, I know in general a way to read text lines ony-by-one from a file into a string variable. > But I miss somehow a short one-liner like: > > foreach(String currentline : file("D:\test\myfile.txt")) { > .... > } > > Is there something like this in Java? > What would be the shortest way otherwise? Well, a BufferedReader is a good way to read a line at a time. It would be trivial to wrap that in an Iterable implementation so that you could use the syntax above. Pete