软件体系结构上机实验 面向对象体系结构风格的 KWIC 关键词索引系统设计与实现 java

来源:互联网 发布:室内设计 知乎 编辑:程序博客网 时间:2024/04/29 20:52





java 源代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class KWIC
{
   private static BufferedReader input_file;//输入缓冲对象
   private static BufferedWriter output_file;//输出缓冲对象
   private ArrayList<String> kwicList;
   public KWIC (String filename)  
    {
        kwicList = new ArrayList<String>();
        String line="";
        fileopen(filename);//打开文件
        while (line!= null)
        {
            line=readline();//每行读取文件
            if (line !=null)//每行不空
            {
                parseLine(line, kwicList);//每行移位
            }
         }
         sort(kwicList);//排序
         writeToFile(kwicList,"output.txt");//结果写入文件
    }
    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++) {//将一行解析,并且将解析的word加入ArrayList中
            token = tokener.nextToken();
            tokens.add(token);//对每一行先读取正常输出
        }
        //对ArrayList中的字进行循环移位,得出最后结果
        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 void sort(ArrayList<String> List)//排序函数
{
  for(int i=List.size()-1;i>0;i--)
  {
  for(int j=0;j<i;j++)
  {
  char a=List.get(j).charAt(0);
  char b=List.get(j+1).charAt(0);
  if(a>b)
  {
  String temp=List.get(j);
  List.remove(j);
  List.add(j+1,temp);
  }
  }
  }
}
/*public static void  display(ArrayList<String> List)//输出结果函数
    {
        
        for (int count=0; count< List.size();count++) {
              System.out.println (List.get(count) );//每行输出
        }
    }
    */
public void writeToFile(ArrayList<String> List,String filename) {


    try {


        
        output_file= new BufferedWriter(new FileWriter(filename));
        for (int count=0; count< List.size();count++)
    {
            output_file.write(List.get(count) );//每行写入文件
            output_file.newLine();//读取下一行
            
        }


       } 
    catch (FileNotFoundException ex)
    {
        ex.printStackTrace();
    } 
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    finally 
    {
        
        try {
              if (output_file != null) 
              {
            output_file.flush();
            output_file.close();//关闭文件
               }
           }
           catch (IOException ex) 
           {
            ex.printStackTrace();
           }
    }
       
}
public static void main(String[] args) //主程序
    {
            new KWIC("input.txt");
    }
}

0 0
原创粉丝点击