我想从文本文件获取的数组中删除第三个值。 我的文本文件如下所示:
item = 0 Dwarf_remains The_body_of_a_Dwarf_savaged_by_Goblins. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 1 Toolkit Good_for_repairing_a_broken_cannon. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 2 Cannonball Ammo_for_the_Dwarf_Cannon. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 3 Nulodion's_notes It's_a_Nulodion's_notes. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 4 Ammo_mould Used_to_make_cannon_ammunition. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 5 Instruction_manual An_old_note_book. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
然后我想删除所有这些东西:
The_body_of_a_Dwarf_savaged_by_Goblins.
Ammo_for_the_Dwarf_Cannon.
It's_a_Nulodion's_notes.
Used_to_make_cannon_ammunition.
An_old_note_book.
我现在使用此代码来获取数组
import java.io.*;
public class Main {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "Items.cfg";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] values = line.split("\\t");
System.out.println(values[2]);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
当java删除所有这些时,是否可以将更新的文件保存在另一个文件中,例如Items2.cfg或其他文件?
最佳答案
例如与此类似?我复制了答案以便于访问。 Removing an element from an Array (Java)
数组 = ArrayUtils.removeElement(数组, 元素)
关于java - 如何从数组中删除一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703864/