The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Your Grade

Posted by Matt Gerrans on January 17, 2002 at 4:45 PM

> mr. lecturer, please grade :p
B-   You asked for it! ;-)

Here's my version (note the use of constants, StringBuffer instead of String and especially the fact that this method returns the correct number of characters (compare the size of your resulting String to the size of the original file)):


static String getTextOfFile( String filename ) throws IOException
{
StringBuffer text = new StringBuffer();
int b, count = 0;
final int MAX_LEN = 255;
final int LINEFEED = 10;
char[] ch = new char[MAX_LEN];

InputStream in = new FileInputStream( filename );
b = in.read();
while( b != -1 )
{
if( b == LINEFEED || count == MAX_LEN)
{
text.append( ch, 0, count );
if( count != MAX_LEN )
text.append("\n");

count=0;
}
else
{
ch[count]=(char)b;
count++;
}
b = in.read();
}

in.close();
return text.toString();
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us