📄 avoidoverwritingfile.java
字号:
// Chapter 9 Exercise 1
// To make it more sensible I added provision for the filepath to be a command line argument.
// *** indicates modified or added code
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class AvoidOverwritingFile {
public static void main(String[] args) {
String filepath = "C:/Beg Java Stuff/myFile.txt";
if(args.length>0) // ***
filepath = args[0]; // ***
File aFile = new File(filepath);
FileOutputStream outputFile = null; // Stores the stream reference
if (aFile.isFile()) {
File newFile = aFile; // Start with the original file
// We will append "_old" to the file
// name repeatedly until it is unique
do {
String name = newFile.getName(); // Get the name of the file
int period = name.indexOf('.'); // Find the separator for the extension
if(period == -1) // ***
newFile = new File(newFile.getParent(), name + "_old"); // ***
else // ***
newFile = new File(newFile.getParent(),
name.substring(0, period) + "_old"
+ name.substring(period));
} while (!aFile.renameTo(newFile)); // Stop when renaming works
}
// Now we can create the new file
try {
// Create the stream opened to append data
outputFile = new FileOutputStream(aFile);
System.out.println(aFile.getName()+" output stream created");
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -