Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
Package org. BUPT. kwic;
Import java. Io. bufferedreader;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. collections;
Import java. util. stringtokenizer;
Public class mykwic {
Private Static bufferedreader input_file;
Private arraylist <string> kwiclist;
Public mykwic (string filename) // construct the index of file fname
{
Kwiclist = new arraylist <string> ();
String line = "";
Fileopen (filename );
While (line! = NULL)
{
Line = Readline ();
If (line! = NULL)
{
Parseline (line, kwiclist );
}
}
// Collections. Sort (kwiclist );
Display (kwiclist );
}
Public static void fileopen (string inputfilename ){
Try {
Input_file = new bufferedreader (New filereader (inputfilename ));
} Catch (ioexception e ){
System. Err. println ("file not open" + E. tostring ()));
System. Exit (1 );
}
}
Public static string Readline (){
String line = "";
Try {
Line = input_file.readline ();
} Catch (exception e ){
E. getstacktrace ();
}
Return line;
}
Public void parseline (string line, arraylist <string> List ){
Stringtokenizer tokener = new stringtokenizer (line );
String token = new string ();
Int index;
Arraylist <string> tokens = new arraylist <string> ();
Int COUNT = tokener. counttokens ();
For (Int J = 0; j <count; j ++) {// parse a row and add the resolved word to the arraylist
Token = tokener. nexttoken ();
Tokens. Add (token );
}
// Shift the words in the arraylist cyclically to obtain the final result
For (INT I = 0; I <count; I ++ ){
Index = I;
Stringbuffer linebuffer = new stringbuffer ();
For (Int J = 0; j <count; j ++ ){
If (index> = count)
Index = 0;
Linebuffer. append (tokens. Get (INDEX ));
Linebuffer. append ("");
Index ++;
}
Line = linebuffer. tostring ();
Kwiclist. Add (line );
}
}
Public static void display (arraylist <string> List ){
System. Out. println ("output is ");
For (int count = 0; count <list. Size (); count ++ ){
System. Out. println (list. Get (count ));
}
}
Public static void main (string [] ARGs ){
New mykwic ("test.txt ");
}
}
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/