从 Java 中的文件生成的数组中删除文本
Removing Text from an Array Made From a File in Java
我对 Java 比较了解,并且对于个人项目,我正在尝试编写一个程序,该程序可以接受用户输入以写入文本文件并从一行中删除文本(由数组的索引)来自从同一文本文件构建的数组,这样当您打印出文件的内容时,您删除的内容确实被删除了。
这是我的代码,对于我看不到的任何其他错误,我深表歉意:
public class Writing {
Scanner input = new Scanner(System.in);
File customerFile = new File("Customers.txt");
String[] fileArray;
public Writing() throws IOException{
FileReader fileReader = new FileReader(customerFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
fileArray = lines.toArray(new String[lines.size()]);
}
public void FileWrite() throws IOException {
String inputData = input.nextLine();
if (!customerFile.exists()) {
customerFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(customerFile.getName(),true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
bufferWriter.write(inputData + "\n");
bufferWriter.close();
System.out.println("Done.");
}
//Here is the one I have trouble with
public void RemoveFile() throws IOException {
LinkedList<String> cusList = new LinkedList<String>(Arrays.asList(fileArray));
System.out.println("Which one would you like to delete? [0-#].");
//then it prints the list so you can see what you want to delete
for(String x: cusList){
System.out.println(x);
}
String removeThis = input.nextLine();
int removeNum = Integer.parseInt(removeThis);
if()){ //if the user input contains the number that occurs as an index in the list
//not sure what to put here
fileArray = cusList.toArray(fileArray);
}else{
System.out.println("Remove failed.");
}
}
}
我被困住了,因为尽管过去曾尝试解决这个问题,但我最终还是收到了我的 else 语句,或者文本文件没有任何变化。
编辑:
以下是我为解决此问题所做的主要尝试,但无济于事。 这是在 RemoveFile() 中 class。
失败的方法:
//tries to remove the element if it is found, before I decided to jusr remove the index
//example: if "Cocoa Powder" is typed in and it exists within the list of the array created from the file, remove it.
if(cusList.contains(removeThis)){
cusList.remove(removeThis);
cusList.toArray(fileArray);
}
//the else statement here saying that it failed
方法二失败:
//attempts to remove the content of the inputted index within the list
if(removeNum.equals(cusList.indexOf(removeNum))){
cusList.remove(removeNum);
cusList.toArray(fileArray);
}
//else statement would go here
求助!
解决方法:
我想通了,感谢所有提供帮助的人,我意识到一个更简单的解决方案是清除文本文件的所有内容,使用包含所有内容的数组,然后将这些内容写回所有更改后的文本文件。这是我的代码:
// RemoveFile method
public void RemoveFile() throws IOException {
LinkedList<String> cusList = new LinkedList<String>(
Arrays.asList(fileArray));
System.out.println("Which one would you like to delete? Each customer is given a number [1-#].");
System.out.println("The file will change when program terminated.");
for (int i = 1; i < fileArray.length; i++) {
for (String x : cusList) {
System.out.println("Customer " + i + ": " + x);
i++;
}
}
String removeThis = input.nextLine();
int removedNum = Integer.parseInt(removeThis);
int removeNum = removedNum - 1;
if (removeNum >= 0 && removeNum < cusList.size()) {
cusList.remove(removeNum);
fileArray = cusList.toArray(fileArray);
PrintWriter writer = new PrintWriter(customerFile);
writer.print("");// clears the file contents
writer.close();
FileWriter fileWriter = new FileWriter(customerFile.getName(), true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
for (String x : cusList) {
bufferWriter.write(x + "\n");
}
bufferWriter.close();
} else {
System.out.println("Index out of bounds, " + removeNum + " >= "
+ cusList.size());
}
这似乎工作得很好而且 MadProgrammer 的建议使逻辑工作,而不是不断地返回 false 然后允许程序从文件中提取数据到一个数组中,清除数组的内容,然后写入它回到文件中。
custList
包含来自文件的 String
值列表,contains
将始终 return false
,因为它正在寻找匹配中的相等性(例如 1 != "some string value"
)。
你需要做一些更像 if (removeNum >= 0 && removeNum < cusList.size())
的事情,然后你知道该值在 List
的范围内,然后你可以简单地使用 removeNum
删除值给定的索引,custList.remove(removeNum)
if (removeNum >= 0 && removeNum < cusList.size()) {
custList.remove(removeNum);
}else{
System.out.println("Index out of bounds, " + removeNum + " >= " + custList.size());
}
File inFile = new File("input.dat");
File outFile = new File("output.dat");
Scanner scan = new Scanner(inFile);
PrintWriter out = new PrintWriter(outFile);
while(scan.hasNextLine()) {
String line = scan.nextLine();
if(<criterion_to_determine_whether_to_keep_line_or_not>) {
out.println(line);
}
}
scan.close();
out.close();
原则上,您不应尝试编辑作为输入收到的文件,而应使用更改后的输出编写一个新文件。如果您确实需要新文件与旧文件同名,请对两个文件执行 File.renameTo()。
我对 Java 比较了解,并且对于个人项目,我正在尝试编写一个程序,该程序可以接受用户输入以写入文本文件并从一行中删除文本(由数组的索引)来自从同一文本文件构建的数组,这样当您打印出文件的内容时,您删除的内容确实被删除了。
这是我的代码,对于我看不到的任何其他错误,我深表歉意:
public class Writing {
Scanner input = new Scanner(System.in);
File customerFile = new File("Customers.txt");
String[] fileArray;
public Writing() throws IOException{
FileReader fileReader = new FileReader(customerFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
fileArray = lines.toArray(new String[lines.size()]);
}
public void FileWrite() throws IOException {
String inputData = input.nextLine();
if (!customerFile.exists()) {
customerFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(customerFile.getName(),true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
bufferWriter.write(inputData + "\n");
bufferWriter.close();
System.out.println("Done.");
}
//Here is the one I have trouble with
public void RemoveFile() throws IOException {
LinkedList<String> cusList = new LinkedList<String>(Arrays.asList(fileArray));
System.out.println("Which one would you like to delete? [0-#].");
//then it prints the list so you can see what you want to delete
for(String x: cusList){
System.out.println(x);
}
String removeThis = input.nextLine();
int removeNum = Integer.parseInt(removeThis);
if()){ //if the user input contains the number that occurs as an index in the list
//not sure what to put here
fileArray = cusList.toArray(fileArray);
}else{
System.out.println("Remove failed.");
}
}
}
我被困住了,因为尽管过去曾尝试解决这个问题,但我最终还是收到了我的 else 语句,或者文本文件没有任何变化。
编辑: 以下是我为解决此问题所做的主要尝试,但无济于事。 这是在 RemoveFile() 中 class。
失败的方法:
//tries to remove the element if it is found, before I decided to jusr remove the index
//example: if "Cocoa Powder" is typed in and it exists within the list of the array created from the file, remove it.
if(cusList.contains(removeThis)){
cusList.remove(removeThis);
cusList.toArray(fileArray);
}
//the else statement here saying that it failed
方法二失败:
//attempts to remove the content of the inputted index within the list
if(removeNum.equals(cusList.indexOf(removeNum))){
cusList.remove(removeNum);
cusList.toArray(fileArray);
}
//else statement would go here
求助!
解决方法: 我想通了,感谢所有提供帮助的人,我意识到一个更简单的解决方案是清除文本文件的所有内容,使用包含所有内容的数组,然后将这些内容写回所有更改后的文本文件。这是我的代码:
// RemoveFile method
public void RemoveFile() throws IOException {
LinkedList<String> cusList = new LinkedList<String>(
Arrays.asList(fileArray));
System.out.println("Which one would you like to delete? Each customer is given a number [1-#].");
System.out.println("The file will change when program terminated.");
for (int i = 1; i < fileArray.length; i++) {
for (String x : cusList) {
System.out.println("Customer " + i + ": " + x);
i++;
}
}
String removeThis = input.nextLine();
int removedNum = Integer.parseInt(removeThis);
int removeNum = removedNum - 1;
if (removeNum >= 0 && removeNum < cusList.size()) {
cusList.remove(removeNum);
fileArray = cusList.toArray(fileArray);
PrintWriter writer = new PrintWriter(customerFile);
writer.print("");// clears the file contents
writer.close();
FileWriter fileWriter = new FileWriter(customerFile.getName(), true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
for (String x : cusList) {
bufferWriter.write(x + "\n");
}
bufferWriter.close();
} else {
System.out.println("Index out of bounds, " + removeNum + " >= "
+ cusList.size());
}
这似乎工作得很好而且 MadProgrammer 的建议使逻辑工作,而不是不断地返回 false 然后允许程序从文件中提取数据到一个数组中,清除数组的内容,然后写入它回到文件中。
custList
包含来自文件的 String
值列表,contains
将始终 return false
,因为它正在寻找匹配中的相等性(例如 1 != "some string value"
)。
你需要做一些更像 if (removeNum >= 0 && removeNum < cusList.size())
的事情,然后你知道该值在 List
的范围内,然后你可以简单地使用 removeNum
删除值给定的索引,custList.remove(removeNum)
if (removeNum >= 0 && removeNum < cusList.size()) {
custList.remove(removeNum);
}else{
System.out.println("Index out of bounds, " + removeNum + " >= " + custList.size());
}
File inFile = new File("input.dat");
File outFile = new File("output.dat");
Scanner scan = new Scanner(inFile);
PrintWriter out = new PrintWriter(outFile);
while(scan.hasNextLine()) {
String line = scan.nextLine();
if(<criterion_to_determine_whether_to_keep_line_or_not>) {
out.println(line);
}
}
scan.close();
out.close();
原则上,您不应尝试编辑作为输入收到的文件,而应使用更改后的输出编写一个新文件。如果您确实需要新文件与旧文件同名,请对两个文件执行 File.renameTo()。