读取文件,每行读取,然后替换指定的内容 删除指定的行数:
public static void ChangeFile (String filePath, String outPath) throws IOException { try { BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));//数据流读取文件
StringBuffer strBuffer = new StringBuffer();
String empty = "";
String tihuan = "";
List<String> arrList = new ArrayList<>();
for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
if(temp.contains("\"PUBLIC\".")) {
temp = temp.replace("\"PUBLIC\"."," ");
}
if(temp.contains("\"")) {
temp = temp.replace("\"","'");
}
arrList.add(temp);
}
//删除22行之前的数据
arrList = arrList.subList(21,arrList.size());
for (String str : arrList){
strBuffer.append(str);
strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
}
bufReader.close();
PrintWriter printWriter = new PrintWriter(outPath);//替换后输出的文件位置
printWriter.write(strBuffer.toString().toCharArray());
printWriter.flush();
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
|