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();
}
}}
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();
}
}}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at fileAnalyze.FileAnalyze.main(FileAnalyze.java:37)
请各位大神指教
if(confirm=="1"){这里改成用equals判断相等.或者也可以用confirm = confirm.intern();
if(confirm == "1"){
// code ....
}