我有一个包含文本文件数据的数组列表。
文本文件的结构如下
1,2,Name,2,itemName
我的代码:
String Cid = "1";
String Tid = "1";
//Help
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
FileWriter writer = new FileWriter(iFile);
String dataRow = yourfile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine();
else{
System.out.print(dataRow);
writer.append(dataArray[0]+", ");
writer.append(dataArray[1]+", ");
writer.append(dataArray[2]+", ");
writer.append(dataArray[3]+", ");
writer.append(dataArray[4]);
writer.append(System.getProperty("line.separator"));
dataRow = yourfile.readLine();
}
}
writer.flush();
writer.close();
} 我希望能够删除Name id和Item id匹配的记录。
我读到的关于从数组列表中删除项目的所有内容仅涉及按项目位置删除。任何帮助将非常感激。
答案 0 :(得分:0)
我认为你需要遍历数组列表中的每个元素,并为每个String创建一个带有“,”作为分隔符的java.util.StringTokenizer(我假设Name或itemName中没有逗号)。
然后获得第2和第4个令牌并进行比较。如果匹配则删除该项目。
如果你使用一个从ArrayList结尾处开始并移动到第0个元素的for循环,在找到它们时按索引删除项目,这可能是最有效的。
答案 1 :(得分:0)
String Cid = "1";
String Tid = "1";
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
BufferedReader yourfile2 = new BufferedReader(new FileReader(iFile));
int total=0;
String rec=yourfile2.readLine();
while (rec != null){ // count total records (rows)
total++;
rec=yourfile2.readLine();
}
String dataRow = yourfile.readLine();
String[] allTemp[]=new String[total][]; //create array of an array with size of the total record/row
int counter=0;
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine(); // skip current row if match found
else{
allTemp[counter]=dataArray; //if match not found, store the array into another array
dataRow = yourfile.readLine();
counter++; //index for allTemp array. note that counter start from zero and no increment if the current row is skipped
}
}
FileWriter writer = new FileWriter(iFile); //create new file which will replace the records file. here, all desired records from file already stored in allTemp array
for (String[] arr : allTemp){
//check nullity of array inside the array(record).
if(arr!=null){
for(int i=0;i<arr.length;i++){
writer.append(arr[i]);
if(i<arr.length-1) //add "," in every column except in the last column
writer.append(",");
}
writer.append(System.getProperty("line.separator"));
}
}
writer.flush();
writer.close();
更新:您可以删除String[] temp;
和temp = new String[dataArray.length];
,因为它实际上从未使用过
答案 2 :(得分:0)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Neat {
public static void main(String... string) throws FileNotFoundException {
File file = new File("c:/AnyFile.txt");
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String text = fileScanner.nextLine();
String[] data = text.split(",");
int recordId = Integer.parseInt(data[0]);
int nameId = Integer.parseInt(data[1]);
String name = data[2];
int itemId = Integer.parseInt(data[3]);
String itemName = data[4];
if (nameId == itemId) {
removeLineFromFile(file, text);
}
}
}
public static void removeLineFromFile(File file, String lineToRemove) {
try {
File inFile = file;
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
// Construct the new file that will later be renamed to the original
// filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
// Read from the original file and write to the new
// unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
// Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
得到你想要的东西