Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26052

Re: Shortest way to read all lines (one by one) from a text file?

From Kevin McMurtrie <mcmurtrie@pixelmemory.us>
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>
Date 2011-02-11 22:15 -0800
Message-ID <4d562585$0$22091$742ec2ed@news.sonic.net> (permalink)
Organization Sonic.Net

Show all headers | View raw


In article <4d54f158$0$6975$9b4e6d93@newsspool4.arcor-online.net>,
 rob@wenger.net (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?
>            
> Robin

The problem with your one-liner example is that it provides 
functionality good for scripting but bad for general purpose 
programming.  Java is a general purpose language so it needs more 
descriptive coding.

final BufferedReader in= new BufferedReader(new FileReader("foo.txt"));
try
{
   String line;
   while ((line= in.readLine()) != null)
      System.out.println(line);
}
finally
{
   in.close();
}

Of course you can write your own Iterable that a for-each style loop 
will accept.  It's late and my mind is foggy so debugging and preventing 
file descriptor leaks is up to you :)

for (String s : new LineReader(new File ("foo.txt")))
         System.out.println(s);


class LineReader implements Iterable<String> 
{
  final File m_file;
  LineReader (final File f)
  {
    m_file= f;
  }
  @Override
  public Iterator<String> iterator()
  {
    try
    {
      final BufferedReader in=
        new BufferedReader(new FileReader(m_file));
      return new Iterator<String>()
      {
        String line= null;
        @Override public boolean hasNext()
        {
          try
          {
            if (line == null)
              line= in.readLine();
            if (line == null)
            {
              in.close();
              return false;
            }
            return true;
          }
          catch (IOException e)
          {
            throw new RuntimeException(e);
          }
        }

        @Override public String next()
        {
          if (!hasNext())
            throw new NoSuchElementException();
          final String rv= line;
          line= null;
          return rv;
        }

        @Override
        public void remove()
        {
          throw new UnsupportedOperationException();
        }
      };
    }
    catch (IOException e)
    {
      throw new RuntimeException(e);
    }
  }
}
-- 
I will not see posts or email from Google because I must filter them as spam

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar


Thread

Shortest way to read all lines (one by one) from a text file? rob@wenger.net (Robin Wenger) - 2011-02-11 08:20 +0000
  Re: Shortest way to read all lines (one by one) from a text file? Jim Janney <jjanney@shell.xmission.com> - 2011-02-11 08:23 -0700
  Re: Shortest way to read all lines (one by one) from a text file? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-02-11 17:01 +0800
  Re: Shortest way to read all lines (one by one) from a text file? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2011-02-11 22:15 -0800

csiph-web