我正在研究項目以讀取,寫入,格式化並更改桌面上預定文件的名稱。除了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的方法,這是你想要的嗎? –