/*
A class containing methods useful for manipulating
Text Files
1. readfilebyline:
Reads the contents of a text file line by line
And returns a string array representation, where
Each line is stored in an element of the array.
2. readfile
Read the contents of a text file into a string
*/
Import java. Io .*;
Import java. util .*;
Public class textmaniptools {
/*------------------------------------------------*/
@ Suppresswarnings ("unchecked ")
Public static string [] readfilebyline (string filename ){
Arraylist filecontents = new arraylist ();
String [] F;
String inputline = new string ();
Try {
Bufferedreader bin = new bufferedreader (
New inputstreamreader (
New fileinputstream (
New file (filename ))));
Int line = 0;
While (inputline = bin. Readline ())! = NULL ){
Filecontents. Add (inputline );
Line ++;
}
}
Catch (exception e ){
System. Out. println ("an error occurred during file reading" + E );
}
F = new string [filecontents. Size ()];
For (INT I = 0; I <filecontents. Size (); I ++ ){
F [I] = (string) filecontents. Get (I );
}
Return (f );
}
/*------------------------------------------------*/
Public static string readfile (string filename ){
Inputstream in = NULL;
Byte inputbuffer [] = NULL ;;
Try {
In = new fileinputstream (new file (filename ));
Inputbuffer = new byte [IN. Available ()];
In. Read (inputbuffer );
}
Catch (exception e ){
System. Out. println ("an error occurred during file reading" + E );
}
Return (new string (inputbuffer ));
}
/* ---- Just to test --------------------------------*/
Public static void main (string [] ARGs ){
String filename = ARGs [0];
// String [] filecontents = textmaniptools. readfilebyline (filename );
String filecontents = textmaniptools. readfile (filename );
System. Out. println (filecontents );
}
}