读书人

有关KWIC系统的有关问题请大家帮帮忙

发布时间: 2012-01-10 21:26:51 作者: rapoo

有关KWIC系统的问题,请大家帮帮忙
前些阵自己用JAVA编写了一个KWIC的管道模式的程序,但是总是处理不了多行的输入(当你的输入文本是多行的时候,就只能处理第一行),单行的就可以,请大家看看,谢谢,哪位高手能帮忙解决下,十分的感谢!!代码如下(如果阅读不便请大家copy下来看看,不好意思!):
import java.lang.*;
import java.io.*;
import java.util.StringTokenizer;
public class KWIC_Pipe
{
public static void main(String[]args)
{
try
{
File file_1=new File( "C:\\Documents and Settings\\jesse\\桌面\\KWIC.txt ");//来源文件
FileReader words=new FileReader(file_1);
Reader KwicWords=sort(shift(words)); //先移位再排序

BufferedReader in=new BufferedReader(KwicWords);
File file_2=new File( "C:\\Documents and Settings\\jesse\\桌面\\KWIC_OUTPUT.txt ");//要将数据写到KWIC_OUTPUT.txt
PrintWriter outFile=new PrintWriter(new FileWriter(file_2));

String inputNew;
while((inputNew=in.readLine())!=null)
{
System.out.println( "结果: "+inputNew);
outFile.println(inputNew);
}
outFile.close();
in.close();
}
catch(IOException ex)
{
System.out.println(ex);
}
}

//移位方法
static Reader shift(Reader source)throws IOException
{
BufferedReader in=new BufferedReader(source);
PipedWriter pipeOut=new PipedWriter();
PipedReader pipeIn=new PipedReader(pipeOut);
PrintWriter out=new PrintWriter(pipeOut);

new ShiftThread(out,in).start();//启动线程做移位工作
return pipeIn;//移位的同时,不等移位结束就开始排序线程
}

//排序方法
static Reader sort(Reader source)throws IOException
{
BufferedReader in=new BufferedReader(source);
PipedWriter pipeOut=new PipedWriter();
PipedReader pipeIn=new PipedReader(pipeOut);
PrintWriter out=new PrintWriter(pipeOut);//建立一个向管道写入数据的PrintWriter实例

new SortThread(out,in).start();//启动线程做移位工作
return pipeIn;//线程启动后就返回不必等排序结束
}
}//class KWIC_Pipe end
//===========================================================================
class ShiftThread extends Thread
{
private PrintWriter out=null;
private BufferedReader in=null;
private int length;
private int i;
//constructor function
public ShiftThread(PrintWriter out,BufferedReader in)
{
this.out=out;
this.in=in;
}
public void run()
{
if(out!=null&&in!=null)
{
try
{
String input;
String shiftItStr;
while((input=in.readLine())!=null)
{
String[]string;
String[]string2;
StringTokenizer st=new StringTokenizer(input);
string=new String[st.countTokens()];
string2=new String[st.countTokens()];
length=st.countTokens();
//赋值
for(i=0;i <length;i++)
{


string[i]=st.nextToken();
}

//开始移位
shiftIt(string,string2,length);

//写入管道
for(i=0;i <length;i++)
{
shiftItStr=string2[i];
out.println(shiftItStr);
}
out.flush();
try
{
Thread.sleep(100);
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
out.close();
}
}
catch(IOException ex)
{
System.out.println(ex);
}
}
}
public String[] shiftIt(String[]str,String[]str2,int length)
{
String s1,s2;
for(int i=0;i <length;i++)
{
StringBuffer strbuf=new StringBuffer();
s1=str[0];
for(int k=0;k <length;k++)
{
if(k==length-1)
str[k]=s1;
else
str[k]=str[k+1];
strbuf.append(str[k]+ " ");
}
s2=strbuf.toString();
str2[i]=s2;
}
return str2;
}
}//class ShiftThread end
//=============================================================================
class SortThread extends Thread
{
private PrintWriter out=null;
private BufferedReader in=null;
public SortThread(PrintWriter out,BufferedReader in)
{
this.out=out;
this.in=in;
}
public void run()
{
int MaxWords=50;
if(out!=null&&in!=null)
{
try
{
String[]listOfWords=new String[MaxWords];
int numwords=0;
while((listOfWords[numwords]=in.readLine())!=null)
{
numwords++;
}

sort(listOfWords,numwords-1);
for(int i=0;i <numwords;i++)
{
out.println(listOfWords[i]);
try
{
Thread.sleep(100);
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
}
out.close();
}
catch(IOException ex)
{


System.out.println(ex);
}
}

}
//sort
public void sort(String[] str,int theHi)
{
String currentMax;
int currentIndex;
for(int i=theHi;i> =1;i--)
{
currentMax=str[i];
currentIndex=i;
for(int j=i-1;j> =0;j--)
{
if(str[j].compareTo(currentMax)> 0)
{
currentMax=str[j];
currentIndex=j;
}
}
if(currentIndex!=i)
{
str[currentIndex]=str[i];
str[i]=currentMax;
}
}
}
}//class SortThread end
//=============================================================================

[解决办法]
关注
[解决办法]
shiftThread 里的out.close(); 移到下一个花括号后边即可


out.flush();
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
System.out.println(ex);
}

} out.close();
}

读书人网 >Eclipse开发

热点推荐