0

我有一个数组列表,其中包含来自文本文件的数据。

文本文件的结构如下

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();

我希望能够删除名称 id 和项目 id 匹配的记录。

我读过的关于从数组列表中删除项目的所有内容都只涉及按项目位置删除。任何帮助将非常感激。

4

3 回答 3

0

我认为您需要遍历数组列表中的每个元素,并为每个字符串创建一个 java.util.StringTokenizer,并以“,”作为分隔符(我假设 Name 或 itemName 中没有逗号)。

然后获取第 2 个和第 4 个令牌并进行比较。如果然后匹配,则删除该项目。

如果您使用从 ArrayList 末尾开始并移动到第 0 个元素的 for 循环,在找到它们时按索引删除项目,这可能是最有效的。

于 2013-10-31T02:32:08.780 回答
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];因为它实际上从未使用过

于 2013-10-31T02:54:33.387 回答
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();
        }
    }


}

只要得到你想要的东西

于 2013-10-31T03:31:33.613 回答