package fileAnalyze;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
//第四次作业-文件分析public class FileAnalyze {
public static String readTxtFile(String filePath){
String txtLine=null;
try{
String encoding="UTF-8";
File file=new File(filePath);

if(file.isFile() && file.exists()){
InputStreamReader isr=new InputStreamReader(new FileInputStream(file),encoding);
BufferedReader br=new BufferedReader(isr);
String tempLine=null;
while((tempLine=br.readLine())!=null){
txtLine=txtLine+tempLine;
System.out.println(txtLine);
}
br.close();
}
else{
System.out.println("没找到相关文件");
}

}catch(Exception e){
System.out.println("文件读取时出错");
e.printStackTrace();
}
return txtLine;
}
public static void main(String args[]) throws IOException{
System.out.println("请将txt文件放在src目录下,确定请输入1");
Scanner in=new Scanner(System.in);
String confirm=in.nextLine();
ArrayList<String> list=new ArrayList<String>();

if(confirm=="1"){
String filePath = "src/fileAnalyze/Article.txt";
        String txtLine=readTxtFile(filePath);
        
        //创建分词对象
        Analyzer analyzer=new IKAnalyzer(true);
        StringReader reader=new StringReader(txtLine);
        
        //分词
        TokenStream ts=analyzer.tokenStream("", reader);
        CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
        while(ts.incrementToken()){
            System.out.print(term.toString()+"|"); 
            list.add(term.toString());
        }
        reader.close();
        
        
        
}

}}

解决方案 »

  1.   

    黑体部分报错
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:  at fileAnalyze.FileAnalyze.main(FileAnalyze.java:37)
    请各位大神指教
      

  2.   


    if(confirm=="1"){这里改成用equals判断相等.或者也可以用confirm = confirm.intern();
    if(confirm == "1"){
     // code  ....
    }
      

  3.   

    还有就是在读取文件信息进行转码的时候最好是获取系统默认的编码来转.不要指定UTF-8.因为不同的系统的默认编码不一样的,会随时出现乱码的可能性的.
      

  4.   

    应该是你的文件路径不对,系统没有读取到这个路径。你debug跟踪下看看文件堵上了没有!