我正在研究项目以读取,写入,格式化并更改桌面上预定文件的名称。除了namechange方法外,一切工作都正常。当我输入更改名称并为其提供新文件时,它会使用旧文本文件的位置填充新文本文件。使用文本文件复制到新的文本文件
更新!我发现了这个问题,但我仍然没有答案。似乎无论将哪些信息放入 Scanner inputfile = new Scanner(“here”); 这里将被打印出来不是文件的内容。因此,当下一步到达时,它读取应该扫描的文件,而不是扫描它的这里的部分,并将其打印到文件变量,然后打印到输出和随后的文件。
package file.io;
import java.io.*;
import java.util.Scanner;
import java.lang.StringBuilder;
public class FileIo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String answer;
int x = 0;
String loop;
while (x < 1) {
int y = 0;
System.out.println("what would you like to do read, write, format, "
+ "or change the name?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Read")) {
read();
} else if (answer.equalsIgnoreCase("write")) {
write();
} else if (answer.equalsIgnoreCase("format")) {
format();
} else if (answer.equalsIgnoreCase("change the name")) {
namechange();
} else {
System.out.println("you entered an invalid function.");
}
while (y < 1) {
System.out.println("would you like to do another opperation?");
loop = keyboard.nextLine();
if (loop.equalsIgnoreCase("no")) {
x++;
y++;
System.out.println("goodbye!");
} else if (loop.equalsIgnoreCase("yes")) {
System.out.println("\n");
y++;
} else {
System.out.println("the possible answers are yes or no, try again.");
}
}
}
}
public static void read() {
try {
String file = "C:\\Users\\danor\\Desktop\\example.txt";
File fileHandle = new File(file);
try (Scanner inputfile = new Scanner(fileHandle)) {
String line;
if (inputfile.hasNextLine()) {
while (inputfile.hasNextLine()) {
line = inputfile.nextLine();
System.out.println(line);
}
System.out.println("\n");
} else {
System.out.println("the file contains nothing.");
}
}
} catch (Exception e) {
System.out.println("something went wrong");
}
}
public static void write() {
Scanner keyboard = new Scanner(System.in);
String print;
try {
String file = "C:\\Users\\danor\\Desktop\\example.txt";
FileWriter fwriter = new FileWriter(file, true);
PrintWriter out = new PrintWriter(fwriter);
System.out.println("what would you like to print?");
print = keyboard.nextLine();
out.println(print);
out.close();
} catch (Exception e) {
System.out.println("something went wrong");
}
}
public static void format() {
try {
String file = "C:\\Users\\danor\\Desktop\\example.txt";
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
System.out.println("file formatted.");
} catch (Exception e) {
System.out.println("something went wrong");
}
}
public static void namechange() {
try {
String fileorig = "C:\\Users\\danor\\Desktop\\example.txt";
String asf;
String line;
Scanner keyboard = new Scanner(System.in);
System.out.println("what do you want the new file to be named.");
StringBuilder file = new StringBuilder();
file.append("C:\\Users\\danor\\Desktop\\");
file.append(keyboard.nextLine());
file.append(".txt");
asf = file.toString();
try {
FileWriter fwriter = new FileWriter(asf, true);
PrintWriter out = new PrintWriter(fwriter);
Scanner inputfile = new Scanner(fileorig);
if (inputfile.hasNextLine()) {
while (inputfile.hasNextLine()) {
line = (inputfile.nextLine());
out.println(line);
out.close();
}
System.out.println("\n");
} else {
System.out.println("the file contains nothing.");
}
} catch (FileNotFoundException | UnsupportedEncodingException e) {
System.out.println("something went wrong with the writing");
}
} catch (Exception e) {
System.out.println("there was something wrong witht the reading");
}
}
}
你似乎是在完成写作之前,过早地关闭'out'。查看[The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)以获得更好的解决方案来处理资源管理 – MadProgrammer
'renameTo()'类File的方法,这是你想要的吗? –