Java: 文件读取/添加到文件程序未按预期工作(逻辑错误)
Java: file reading / adding to file program not working as intended (logical error)
我正在尝试编写一个程序:
- 通过用户给定的文件路径扫描文件
- 数其数
- 将其内容打印到终端
- 通过用户给定的另一个文件路径在新文件中添加
而且它还可以统计新文件的字数,并将其内容打印到终端。
但是这个新文件有两个问题:
1-它只保存我的添加而没有旧文件的内容(即使我使用了 .concat() 方法)
2- 它无法计算新文件的字数。
除此之外,我尝试了很多方法来解决这些问题,但我不能...
package com;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) throws IOException {
Scanner scanTwo = new Scanner(System.in);
System.out.println("Please Enter Your File Path");
String filePath = scanTwo.nextLine();
File fileInput = new File(filePath);
Scanner fileScanner = new Scanner(fileInput);
System.out.println(fileScanner.nextLine());
System.out.println("Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words ADD.TO.FILE --> add to selected file");
System.out.println("Type Command:");
Scanner scan = new Scanner(System.in);
String printFileCommand = scan.nextLine();
if (printFileCommand.contains("PRINT.FILE")) {
while (fileScanner.hasNextLine()) {
System.out.println(fileScanner.nextLine());
}
} else if (printFileCommand.contains("COUNT.WORDS")) {
int wordCount = 0;
while (fileScanner.hasNext()) {
String fileWords = fileScanner.next();
wordCount++;
}
System.out.println(wordCount);
} else if (printFileCommand.contains("ADD.TO.FILE")) {
System.out.println("Please Enter Your new file path");
String newFilePath = scan.nextLine();
FileWriter addToFile = new FileWriter(newFilePath);
System.out.println("Please Enter Your Additions: ");
String newFileContent = scan.nextLine();
File newFileInput = new File(newFilePath);
Scanner newFileScanner = new Scanner(newFileInput);
while (newFileScanner.hasNextLine()) {
newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
}
addToFile.write(newFileContent);
addToFile.close();
System.out.println("Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words");
System.out.println("Please Enter Your Command:");
String newCommand = scan.nextLine();
if (newCommand.contains("PRINT.FILE")) {
while (newFileScanner.hasNextLine()) {
System.out.println(newFileScanner.nextLine());
}
} else if (newCommand.contains("COUNT.WORD")) {
int newWordCount = 0;
while (newFileScanner.hasNext()) {
String newFileWords = newFileScanner.next();
newWordCount++;
}
System.out.println(newWordCount);
} else {
System.out.println("COMMAND INVALID!");
}
addToFile.close();
// newFileScanner.close();
}
else {
System.out.println("COMMAND INVALID!");
}
scanTwo.close();
fileScanner.close();
scan.close();
}
}
您确定打开旧文件进行读取和连接吗?我看到你通过 newFilePath
路径打开新文件,但这个文件是空的,这是你的代码:
File newFileInput = new File(newFilePath);
Scanner newFileScanner = new Scanner(newFileInput);
while (newFileScanner.hasNextLine()) {
newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
}
此外,如果您已经使用 Scanner
读取文件,则需要创建新的 Scanner
实例 (Resetting a .nextLine() Scanner)
我正在尝试编写一个程序:
- 通过用户给定的文件路径扫描文件
- 数其数
- 将其内容打印到终端
- 通过用户给定的另一个文件路径在新文件中添加
而且它还可以统计新文件的字数,并将其内容打印到终端。
但是这个新文件有两个问题: 1-它只保存我的添加而没有旧文件的内容(即使我使用了 .concat() 方法) 2- 它无法计算新文件的字数。
除此之外,我尝试了很多方法来解决这些问题,但我不能...
package com;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) throws IOException {
Scanner scanTwo = new Scanner(System.in);
System.out.println("Please Enter Your File Path");
String filePath = scanTwo.nextLine();
File fileInput = new File(filePath);
Scanner fileScanner = new Scanner(fileInput);
System.out.println(fileScanner.nextLine());
System.out.println("Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words ADD.TO.FILE --> add to selected file");
System.out.println("Type Command:");
Scanner scan = new Scanner(System.in);
String printFileCommand = scan.nextLine();
if (printFileCommand.contains("PRINT.FILE")) {
while (fileScanner.hasNextLine()) {
System.out.println(fileScanner.nextLine());
}
} else if (printFileCommand.contains("COUNT.WORDS")) {
int wordCount = 0;
while (fileScanner.hasNext()) {
String fileWords = fileScanner.next();
wordCount++;
}
System.out.println(wordCount);
} else if (printFileCommand.contains("ADD.TO.FILE")) {
System.out.println("Please Enter Your new file path");
String newFilePath = scan.nextLine();
FileWriter addToFile = new FileWriter(newFilePath);
System.out.println("Please Enter Your Additions: ");
String newFileContent = scan.nextLine();
File newFileInput = new File(newFilePath);
Scanner newFileScanner = new Scanner(newFileInput);
while (newFileScanner.hasNextLine()) {
newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
}
addToFile.write(newFileContent);
addToFile.close();
System.out.println("Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words");
System.out.println("Please Enter Your Command:");
String newCommand = scan.nextLine();
if (newCommand.contains("PRINT.FILE")) {
while (newFileScanner.hasNextLine()) {
System.out.println(newFileScanner.nextLine());
}
} else if (newCommand.contains("COUNT.WORD")) {
int newWordCount = 0;
while (newFileScanner.hasNext()) {
String newFileWords = newFileScanner.next();
newWordCount++;
}
System.out.println(newWordCount);
} else {
System.out.println("COMMAND INVALID!");
}
addToFile.close();
// newFileScanner.close();
}
else {
System.out.println("COMMAND INVALID!");
}
scanTwo.close();
fileScanner.close();
scan.close();
}
}
您确定打开旧文件进行读取和连接吗?我看到你通过 newFilePath
路径打开新文件,但这个文件是空的,这是你的代码:
File newFileInput = new File(newFilePath);
Scanner newFileScanner = new Scanner(newFileInput);
while (newFileScanner.hasNextLine()) {
newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
}
此外,如果您已经使用 Scanner
读取文件,则需要创建新的 Scanner
实例 (Resetting a .nextLine() Scanner)