其实我也不懂,但是我在百度文库那儿帮你搜了下文档,你看看可行不。
import java.io.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextSY extends JFrame implements ActionListener {
public static void main(String[] args) {
TextSY mf = new TextSY();
}
JTextField tf = null;
public TextSY() {
// 搜索器的图形界面
super("倒排索引建立");
this.setLayout(new FlowLayout());
this.setSize(300, 200);
this.setBackground(new Color(200, 200, 200));
this.setResizable(false);
// 窗口大小不能改变
//将窗口放在屏幕中间的方法
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
tf = new JTextField("d:/源文件.txt", 18);
JButton b = new JButton("进行倒排索引");
// JButton b_scanfile = new JButton("浏览文件"); //设置浏览文件功能按钮
this.add(new Label("文件名:"));
this.add(tf);
// this.add(b_scanfile);
this.add(b);
b.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
ArrayList a1 = null; // 用于存放导入的数据 加上了
ArrayList a2 = null; // 用于存放排序后的数据
ArrayList a3 = null; // 用于存放合并后的数据
a1 = addNumber(); // 将文件中的内容加上行数
System.out.println("排序后数据");
a2 = sortNumber(a1,0,a1.size()-1);//给ArrayList排序
for (int i = 0; i < a2.size(); i++) { // 得到ArrayList的大小 用size()方法
System.out.println(a2.get(i)); // 得到ArrayList中第i个数的值的方法为get(i)
}
System.out.println("倒排结果");
a3 = deleNumber(a2);
for (int i = 0; i < a3.size(); i++) { // 得到ArrayList的大小 用size()方法
System.out.println(a3.get(i)); // 得到ArrayList中第i个数的值的方法为get(i)
}
print(a3);
}
public void print(ArrayList a){
System.out.println("OK");
PrintWriter pw;
try {
pw = new PrintWriter(new FileWriter( new File("d:/倒排索引结果.txt") ));
for (int i = 0; i < a.size(); i++) { // 得到ArrayList的大小 用size()方法
pw.println(a.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 将文件中的内容加上行数
public ArrayList addNumber() {
ArrayList al = null;
FileInputStream fi = null;
InputStreamReader isr = null;
BufferedReader dis = null;
try {
fi = new FileInputStream(tf.getText());
isr = new InputStreamReader(fi);
dis = new BufferedReader(isr);
}
catch (FileNotFoundException e1) {
// ta.setText("找不到你所需要的文件!");
System.out.println("找不到你所需要的文件!");
} catch (IOException e2) {
System.out.println("输入或输出有误!");
e2.printStackTrace();
}
try {
int count = 1;
String ss = "";
al = new ArrayList(); // 用于存放读入的数据 加上了序号
String s = dis.readLine();
while (s!=null) {
for (int i = 0; i < s.length(); i++) {
if( (s.charAt(i)>='a'&&s.charAt(i)<='z') ||
(s.charAt(i)>='A'&&s.charAt(i)<='Z') ||
s.charAt(i)==' '){
if (i!=s.length()-1 && s.charAt(i) != ' ') { // i!=s.length()-1 是用于判断是否是最后一个字符串 如果是就挑到下面
ss = ss + s.charAt(i);
}
else {
if(i==s.length()-1 && s.charAt(i)!=' '){// 当为最后一个字符串时 没有把最后一个字母加上就挑到这里 所以要加上
ss=ss+s.charAt(i);
}if(ss!=""){ // 排除多余的空格干扰
ss = ss + " " + count;
al.add(ss);
}
ss = "";
}
}
}
count++;
s = dis.readLine();
}
dis.close();
} catch (IOException e2) {
System.out.println("输入或输出有误!");
e2.printStackTrace();
}
return al;
}
//给ArrayList排序
public ArrayList sortNumber(ArrayList a, int low, int high) {
int n = a.size();
int i = low;
int j = high;
String temp = a.get(low).toString();
while (i < j) {
while (i < j && a.get(j).toString().compareTo(temp) > 0) {
j--;
}
if (i < j) {
//a[i] = a[j];
a.set(i,a.get(j));
i++;
}
while (i < j && a.get(i).toString().compareTo(temp) <= 0) {
i++;
}
if (i < j) {
//a[j] = a[i];
a.set(j,a.get(i));
j--;
}
}
//a[i] = temp;
a.set(i,temp);
if (i > low)
sortNumber(a, low, i - 1);
if (i < high)
sortNumber(a, i + 1, high);
return a;
}
//删除ArrayList中的相同项 合并相同的项
public ArrayList deleNumber(ArrayList a){
a.add(" ");
ArrayList b=new ArrayList();
String resultStr=null; //合并后的值存放在这个字符串中
for(int i=0;i
String p="" ,pNumber="",pnext="",pnextNumber=""; // p 是用来存放当前的字符串 pNumber是用来存放字符串的标号
String pstr,pstrNext;
if(i+1 < a.size()){
pstr=a.get(i).toString();
pstrNext=a.get(i+1).toString();
}
else
break;
if(pstr.equals(pstrNext)){ // 如果两个字符串完全相同
continue;
}
else
{
int m=0;
while(pstr.charAt(m)!=' '){
p=p+pstr.charAt(m);
m++;
}
for(int mm=m;mm
pNumber=pNumber+pstr.charAt(mm);
}
int n=0;
while(pstrNext.charAt(n)!=' '){
pnext=pnext+pstrNext.charAt(n);
n++;
}
for(int nn=n;nn
ext.length();nn++){
pnextNumber=pnextNumber+pstrNext.charAt(nn);
}
if(p.equals(pnext)){ // 如果字符串相同,数值不同
if(resultStr==null){ //第一次出现字符串重复
resultStr = pstr+pnextNumber;
}
else{ //多次出现前面的字符串重复 数字不同
resultStr = resultStr +pnextNumber;
}
}
else{
if(resultStr==null){ //字符串不相同时,且只出现一次
b.add(pstr);
}
else{
b.add(resultStr); // 字符串不相同时,且前一个字符串出现了多次
resultStr=null;
}
}
}
}
return b;
}
}