问题
想实现把txt文件里面所有的0001,0002,`9998,9999全部替换成第"x"章
具体代码
try {
int count = 0;
File f = new File("/storage/sdcard0/Download/rrt.txt");
File ff = new File("/storage/sdcard0/Download/rrt2.txt");
InputStream input = new FileInputStream(f);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(input));
StringBuffer strBuf = new StringBuffer();
String tmp;
for (tmp = null; (tmp = bufReader.readLine()) != null; tmp = null) {
// 行数
while(tmp != null) {
count++;
tmp = bufReader.readLine();
}
// 替换全部
// tmp = tmp.replaceAll("\\*", "#");
// 替换第一个相匹配的项
// tmp = tmp.replaceFirst("===", "\\* ");
int a;
for (a = 0; a <= count; a++) {
String bb = Integer.toString(a);
tmp = tmp.replaceAll(bb, "第" + bb + "章");
System.out.println(a);
}
strBuf.append(tmp);
strBuf.append(System.getProperty("line.separator"));
}
bufReader.close();
PrintWriter printWriter = new PrintWriter(ff);
printWriter.write(strBuf.toString().toCharArray());
printWriter.flush();
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在for 替换中报java.lang.NullPointerException
报错信息
at Main.main(Main.java:43)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
at java.lang.Thread.run(Thread.java:841)
这是什么原因?