📄 alice.java
字号:
import java.io.*;
import java.util.regex.*;
import javax.swing.JOptionPane;
public class alice {
public static void main(String[] args) throws IOException{
String fileName;
String filePath;
filePath=JOptionPane.showInputDialog("请输出文件的路径:");
fileName=JOptionPane.showInputDialog("请输出文件的名字:");
File name=new File(filePath, fileName);
try{
BufferedReader buf = new BufferedReader(new FileReader(name));
StringBuffer sbuf = new StringBuffer();//缓冲字符串
String line = null;
while((line = buf.readLine())!=null){
sbuf.append(line);//追加到缓冲字符串中
}
buf.close();//读取结束
String str = sbuf.toString();
Pattern p = Pattern.compile("Alice");//定义正则表达式匹配单词
Matcher m = p.matcher(str);
/* StringBuffer sb = new StringBuffer();*/
int i=0;
boolean result = m.find();
while(result) {
i++;
result = m.find();
}
System.out.println(i);
JOptionPane.showMessageDialog(null , "Alice的数目是:"+i, "结果", JOptionPane.PLAIN_MESSAGE);
}
catch(IOException ioEception){
JOptionPane.showMessageDialog(null , "文件不存在或路径错误或文件没有带上后缀", "错误", JOptionPane.PLAIN_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -