Java 使用扫描程序重命名文件
我需要在以后的代码中重命名文件Java 使用扫描程序重命名文件,java,intellij-idea,java.util.scanner,java-io,printstream,Java,Intellij Idea,Java.util.scanner,Java Io,Printstream,我需要在以后的代码中重命名文件temp.txt。我试着用newFile=newFile(name)来做这件事行,但这只是完全创建了一个新文件。你能给我指一下这里的正确方向吗 public static File InputOutput (Scanner console, int in_Out) throws FileNotFoundException { //<editor-fold> String name = "newName"; Stri
temp.txt
。我试着用newFile=newFile(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;
}
publicstaticfileinputoutput(扫描器控制台,int-in\u-Out)抛出
FileNotFoundException{
//
String name=“newName”;
字符串行=”;
字符串in_OutText=“”;
File newFile=新文件(“temp.txt”);
对于(int i=0;i Java的file类有一个renameTo方法:通常我建议使用Java.nio.file API,因为它更易于处理(例如,更清晰的异常,而不是返回布尔值的方法)。以下是完成此任务的不同方法:谢谢,但我只允许使用扫描仪来完成此任务。也许我只需要重新评估完成此任务的过程,然后重新开始。Java的file类有一个renameTo方法:通常我建议使用Java.nio.file API,因为它更适合使用(例如,更清晰的异常,而不是返回布尔值的方法)。以下是实现这一点的不同方法:谢谢,但我只允许使用扫描仪来完成此操作。也许我只需要重新评估完成此任务的过程,然后重新开始。