import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class file {
int line=0;//记录行数
String str="";//存放文件读出的内容
String word;//存放查找的单词
int count;//记录单词出现的次数
public void newFile() throws IOException {
System.out.println("请输入文件地址:");
Scanner scanner=new Scanner(System.in);
String path =scanner.next();
File file = new File(path);
if(!file.exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
// write
boolean flag=false;
System.out.println("是否输入文本(true/false)");
flag=scanner.nextBoolean();
if(flag==true) {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("请输入文件内容:");
System.out.println("提示:每个单词间需空一格");
Scanner s = new Scanner(System.in);
String content = s.nextLine();
bw.write(content);
bw.flush();
bw.close();
fw.close();
System.out.println("文件创建成功");
}
}
public void Lookup() throws IOException{
System.out.println("请输入文件地址:");
Scanner scanner=new Scanner(System.in);
String path =scanner.next();
File file = new File(path);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
System.out.println("请输入要查询的单词:");
word=scanner.next();
char[]w=word.toCharArray();
while((str=br.readLine())!=null) {
line++;
char[]st=str.toCharArray();
if(KMP(st,w)==true) { //查询单词位置,true表示该单词出现在这一行
System.out.println("在第" + line + "行");
}
}
br.close();
fr.close();
}