以下是代码:
public static void main (String[] args) {
int db2Name;
String fname = "c:\\1.txt";
File f = new File(fname);
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
Pattern p = Pattern.compile("\\W(s.([0-9])*?)\\W");
Matcher m = p.matcher(s);
System.out.println("b");
while (s!=null){
while (m.find()) {
System.out.println(m.group(1).substring(1, 8));
db2Name=Integer.parseInt(m.group(1).substring(1, 8));
} s= br.readLine();
}
br.close();// 关闭缓冲读入流及文件读入流的连接.
System.out.println("a");
} catch (FileNotFoundException e1) {
System.err.println("File not found: " + fname);
} catch (IOException e2) {
e2.printStackTrace();
}
}
在1.TXT中
数据库 sacl1 的数据库配置 数据库配置发行版级别 = 0x0a00 数据库
发行版级别 = 0x0a00 数据库地域
这样就可以读取s开头的字母了。
在2.TXT中
数据库 sacl1 的数据库配置
数据库配置发行版级别 = 0x0a00
数据库发行版级别 = 0x0a00
数据库地域 = CN
数据库代码页 = 1386
这样就找不到S开头的字符了这个是为什么啊?
还是想请问你下,你说:
//而你的2.txt中的第一行为空,所以后面while (s!=null)个循环就不执行了。
但我在while (s!=null){System.out.println("c");//这里加了个输出C我看都执行的么!我到觉得
while (m.find())这个条件好像有点问题不执行了!这该怎么办啊
System.out.println(m.find());一个主要原因是你的正则表达式的错误吧。来了百度hi我,我帮你看看
就是在两个TXT文件中。他们格式不同。1.TXT中我能找到S开头的一个词组。但2.txt中却找不到。这个是为什么啊?