我想替换文件中的字符并将内容复制到临时文件并将临时文件重命名为原始文件。
path = "/mnt/sdcard/TESTfile/"
File _f = new File(path);
File[] _f1 = _f.listFiles();
String[] s1 = { "t1.txt", "t2.txt", "t3.txt" };
_fOut = new FileOutputStream(path + File.separator + s1[i]);
// Copy the bits from input stream to output stream
StringBuffer sb = null;
BufferedWriter out = null;
while ((line = reader.readLine()) != null) {
line = line.replaceAll("a"," ").replaceAll("g"," ");
sb = new StringBuffer();
Log.i("Line>>>" + line, "<<<<<");
sb.append(line);
s = sb.toString();
byte[] temp = s.getBytes();
_fOut.write(temp);
}
fis.close();
_fOut.close();
//old file name with path
String path1 = path + File.separator + s1[i];
//new file name with path
String rename1 = path + File.separator + _f1[i].getName();
File oldFile = new File(path1);
File renameFile = new File(rename1);
for(int j = 0; j < _f1.length;j++)
{
oldFile.renameTo(renameFile);
}
这就是我试图做到的方式。我不确定如何重命名文件并删除原始文件。请帮忙