Hi,
The program do these things:
1.read the source file line by line;
2.modify the
string;
3.write the string to a temp file;
3.rename the temp to the source file.
But I found sometimes the method renameTo() would return false. Would you pls tell me why? What is the mechanism of file's renaming? When it return false and when do true? Thank you !
The code lists below:
try {
//turn the source file f to the Reader
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
//turn the temp file temp to the writer
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(temp))),true);
String s = new String();
while ( (s = in.readLine()) != null) {
s = modifyString(s);//invoke my own method
//to modify string
out.println(s);
}
in.close();
out.close();
System.out.println(temp.renameTo(f));//rename temp file to the
//original file
System.out.println(temp.getPath());
System.out.println(f.getPath());
}
catch (FileNotFoundException fnfe) {}
catch (IOException e) {}