1、 /Alphabetizer.java import java.util.ArrayList; import java.util.Arrays; public class Alphabetizer public ArrayList ls; public Alphabetizer(ArrayList ls) this.ls=ls; public void alpha() String tmpArray = new Stringls.size(); ls.toArray(tmpArray); Arrays.sort(tmpArray); for(int i=0;ils.size();i+) ls.
2、set(i, tmpArrayi); /CircularShifter.java import java.util.ArrayList; public class CircularShifter public ArrayList ls; public CircularShifter(ArrayList ls) this.ls=ls; public void shift() ArrayList shiftedLineIndexes=new ArrayList(); for(int i=0;ils.size();i+) String orinLine=ls.get(i); String sarra
3、y=orinLine.split( ); for(int j=0;j1) if(j=sarray.length-1) for(int k=0;k(sarray.length-1);k+) newLine=newLine+ +sarrayk; else for(int k=j+1;ksarray.length;k+) newLine=newLine+ +sarrayk; for(int m=0;mj;m+) newLine=newLine+ +sarraym; shiftedLineIndexes.add(newLine); ls=shiftedLineIndexes; /Main.java i
4、mport java.util.ArrayList; public class Main public static void main(String args) / TODO Auto-generated method stub ArrayListls=new ArrayList(); InputStore inputStore=new InputStore(ls); inputStore.input(input.txt); CircularShifter cs=new CircularShifter(ls); cs.shift(); Alphabetizer alp=new Alphabe
5、tizer(cs.ls); alp.alpha(); Output output=new Output(alp.ls); output.output(output.txt); KWIC 基于管道过滤器风格的 JAVA 语言实现 /Main类 package kwic_pipe; import java.io.File; import java.util.Scanner; public class Main public static void main(String args) File infile = new File(e:mykwic_in.txt); File outfile = ne
6、w File(e:mykwic_out.txt); Scanner inputfile; Scanner outputfile; try inputfile = new Scanner(infile); outputfile = new Scanner(outfile); / 定义三个管道 Pipe pipe1 = new Pipe(); Pipe pipe2 = new Pipe(); Pipe pipe3 = new Pipe(); / 定义四种过滤器 Input input = new Input(infile, pipe1); Shift shift = new Shift(pipe1
7、, pipe2); Output output = new Output(pipe3, outfile); / 启动四种过滤器的线程 input.transform() shift. transform (); output. transform (); / 直接输出结果 System.out.println(- infile -); String str = null; while (inputfile.hasNextLine() str = inputfile.nextLine(); System.out.println(str); System.out.println(input end
8、); Thread.sleep(3000); System.out.println(- outfile -); while (outputfile.hasNextLine() System.out.println(str); inputfile.close(); outputfile.close(); catch (Exception e) e.getMessage(); /Filter类 package kwic_pipe; import java.io.IOException; public abstract class Filter /定义输入管道 protected Pipe inpu
9、t; /定义输出管道 protected Pipe output; private boolean isStart = false; Filter(Pipe input, Pipe output) this.input = input; this.output = output; / 防止多次调用,调用之后线程开始执行 public void start() if(!isStart) isStart = true; Thread thread = new Thread(); thread.start(); /线程的 run 方法 public void run() try this.trans
10、form(); catch (IOException e) e.getMessage(); /将输入数据转换为所需数据并写入输出管道 /由子类实现抽象方法 protected abstract void transform()throws IOException; Pipe类 package kwic_pipe; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; import java.io.PrintWriter; import java.util.Scanner; publ
11、ic class Pipe /输入管道 private Scanner pipereader; /输出管道 private PrintWriter pipewriter; public Pipe() PipedWriter pw = new PipedWriter(); PipedReader pr = new PipedReader(); try pw.connect(pr); catch (IOException e) e.getMessage(); pipewriter = new PrintWriter(pw); pipereader = new Scanner(pr); /读入一行数
12、据到管道 /return 读入的数据 public String readerLine() throws IOException return pipereader.nextLine(); /从管道输出一行数据 public void writerLine(String strline) throws IOException pipewriter.println(strline); /将读管道关闭,调用该方法后,不能再从管道中读数据 /如不能关闭则抛出异 public void closeReader() throws IOException pipereader.close(); /先刷新数
13、据,在将写管道关闭,调用该方法后,不能向管道中写数据 /如不能关闭则抛出异常 public void closeWriter() throws IOException pipewriter.flush(); pipewriter.close(); Alphabetizer类 package kwic_pipe; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Alphabetizer extends Filter private ArrayLis
14、t al = new ArrayList(); Alphabetizer(Pipe input, Pipe output) super(input, output); /对读入的数据进行排序 protected void transform() throws IOException String templine = null; /读入数据 while(templine = input.readerLine() != null) al.add(templine); /按字母表排序 Collections.sort(al); /对排序后的数据进行输出 for(int i = 0; i al.si
15、ze(); i+) output.writerLine(al.get(i); input.closeReader(); output.closeWriter(); Shift类 package kwic_pipe; import java.io.IOException; import java.util.ArrayList; public class Shift extends Filter /单词的列表 private ArrayList wordlist = new ArrayList(); /重组后的行的列表 private ArrayList linelist = new ArrayList(); Shift(Pipe input, Pipe output) super(input, output); Override protected void transform() throws IOException String templine = ; /读数据 while(templine = input.readerLine() != null)