我将该错误作为“未报告的异常ioexception;必须保留或声明为被抛出”。这是提供的路径中的错误或试图捕获块中的错误。
import java.io.*;
import java.util.regex.*;
class RegexMobileExtractor {
public static void main(String[] args) {
try {
Pattern p = Pattern.compile("(0|9)?[7-9][0-9]{9}");
PrintWriter pw = new PrintWriter("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\output.txt");
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\input.txt"));
//PrintWriter pw = new PrintWriter("output.txt");
//BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line = br.readLine();
while( line!= null) {
Matcher m = p.matcher(line);
while(m.find()) {
pw.println(m.group());
}
line = br.readLine();
}
pw.flush();
pw.close();
//br.close();
} catch (FileNotFoundException obj) {
System.out.println("errr occured");
}
}
}发布于 2017-11-23 14:29:45
这一行代码:br.readLine();可能抛出IOException。这是chacked exception,编译器强制您处理它,这就是为什么您必须添加额外的catch块:
catch (IOException e) {
e.printStackTrace();
}发布于 2017-11-23 15:00:34
换行
} catch (FileNotFoundException obj) {
System.out.println("errr occured");
}至
} catch (IOException obj) {
System.out.println("errr occured");
}因为IOException是FileNotFoundException类的父类,所以您将处理这两种异常。
另外,考虑使用试着用资源来自动关闭您的文件。
https://stackoverflow.com/questions/47457680
复制相似问题