如何将临时文本文件重命名为主文本文件并删除以前存在的主文件
How to rename temporary text file to master text file and delete privious existed master file
此代码将更新的记录插入到临时文件中,插入后必须将 temporary.txt 重命名为主文件,因为它有更新的记录。但是那时我想删除原始的主文本。怎么办?存款模块。
public void insertIntoTempoFile(String uID,String uName,String mobNo,String depoAmt)
{ File f1= new File("C:\Users\hp\Desktop\BankReport\temporary.txt");
File f2= new File("C:\Users\hp\Desktop\BankReport\master.txt");
FileWriter fwTemp = null;
try {
f1.createNewFile();
fwTemp = new FileWriter(f1,true);
fwTemp.write(uID);
fwTemp.write("#");
fwTemp.write(uName);
fwTemp.write("#");
fwTemp.write(mobNo);
fwTemp.write("#");
fwTemp.write(depoAmt);
fwTemp.write("\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fwTemp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
下面的代码将数据写入temporary.txt
,如果没有异常,则将文件重命名为master.txt
。
public static void main(String[] args) {
insertIntoTempoFile("123", "Garreth", "321", "345");
}
private static void insertIntoTempoFile(String uID, String uName, String mobNo, String depoAmt) {
String directory = "C:\Users\hp\Desktop\BankReport\";
String sourceFilename = "temporary.txt";
String targetFilename = "master.txt";
Path sourcePath = Paths.get(directory + sourceFilename);
String row = uID + "#" + uName + "#" + mobNo + "#" + depoAmt + "\n";
try {
Files.write(sourcePath, row.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
renameMasterFileIfExists(directory, sourceFilename, targetFilename);
}
}
/**
* Take a look here for more details https://mkyong.com/java/how-to-rename-file-in-java/
*/
private static void renameMasterFileIfExists(String directory, String sourceFilename, String targetFilename) {
Path source = Paths.get(directory + sourceFilename);
Path target = Paths.get(directory + targetFilename);
try {
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
此代码将更新的记录插入到临时文件中,插入后必须将 temporary.txt 重命名为主文件,因为它有更新的记录。但是那时我想删除原始的主文本。怎么办?存款模块。
public void insertIntoTempoFile(String uID,String uName,String mobNo,String depoAmt)
{ File f1= new File("C:\Users\hp\Desktop\BankReport\temporary.txt");
File f2= new File("C:\Users\hp\Desktop\BankReport\master.txt");
FileWriter fwTemp = null;
try {
f1.createNewFile();
fwTemp = new FileWriter(f1,true);
fwTemp.write(uID);
fwTemp.write("#");
fwTemp.write(uName);
fwTemp.write("#");
fwTemp.write(mobNo);
fwTemp.write("#");
fwTemp.write(depoAmt);
fwTemp.write("\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fwTemp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
下面的代码将数据写入temporary.txt
,如果没有异常,则将文件重命名为master.txt
。
public static void main(String[] args) {
insertIntoTempoFile("123", "Garreth", "321", "345");
}
private static void insertIntoTempoFile(String uID, String uName, String mobNo, String depoAmt) {
String directory = "C:\Users\hp\Desktop\BankReport\";
String sourceFilename = "temporary.txt";
String targetFilename = "master.txt";
Path sourcePath = Paths.get(directory + sourceFilename);
String row = uID + "#" + uName + "#" + mobNo + "#" + depoAmt + "\n";
try {
Files.write(sourcePath, row.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
renameMasterFileIfExists(directory, sourceFilename, targetFilename);
}
}
/**
* Take a look here for more details https://mkyong.com/java/how-to-rename-file-in-java/
*/
private static void renameMasterFileIfExists(String directory, String sourceFilename, String targetFilename) {
Path source = Paths.get(directory + sourceFilename);
Path target = Paths.get(directory + targetFilename);
try {
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}