我需要稍后在我的代码中重命名文件temp.txt
。我尝试使用newFile = new File(name);
行执行此操作,但这只是完全创建了一个新文件。你能指点我在正确的方向吗?
public static File InputOutput (Scanner console, int in_Out) throws
FileNotFoundException {
//<editor-fold>
String name = "newName";
String line = "";
String in_OutText = "";
File newFile = new File("temp.txt");
for (int i = 0; i <= in_Out; i++) {
if (i == 0) {
in_OutText = "Input";
} else {
in_OutText = "Output";
}
System.out.print(in_OutText + " file name: ");
name = console.next();
if (i == 1) {
newFile = new File(name);
}
}
PrintStream inputCopy = new PrintStream(newFile)
Scanner input = new Scanner(name);
while (input.hasNextLine()) {
line = input.nextLine();
inputCopy.println(line);
inputCopy.println("WORKED");
}
return newFile;
}
谢谢!