在.txt文件中查找所有字符串“ID”

时间:2022-09-11 19:27:27

How do I find all string "ID" in an existed .txt file and get how many strings were found?

如何在现有的.txt文件中找到所有字符串“ID”并获取找到多少个字符串?

I have a .txt file like:

我有一个.txt文件,如:

Product ID = "001", Product Name = "P1"

Product ID = "002", Product Name = "P2"

Product ID = "003", Product Name = "P3"

 ...

I would like to add Product ID = "LASTEST_ID_PLUS_1, Product Name = "new" at the end of file, but don't know how to get the last ID number.

我想在文件末尾添加Product ID =“LASTEST_ID_PLUS_1,Product Name =”new“,但不知道如何获取最后一个ID号。

3 个解决方案

#1


2  

String filename = "test.txt";
int numOfIds = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
    String line = null;
    while ((line = br.readLine()) != null) {
        if (line.contains("Product ID = ")) {
            numOfIds++;
        }
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Should be a good starting point. Save up the last read ID and then use that for appending.

应该是一个很好的起点。保存最后读取的ID,然后使用它进行追加。

Edit: After looking at your question again, I'm not even sure why your .txt file has Product ID =in it. Make the txt file look like this for much easier handling:

编辑:再次查看您的问题后,我甚至不确定您的.txt文件中是否包含产品ID =。使txt文件看起来像这样更容易处理:

001,P1
002,P2
003,P3

unless you didn't show the entire file and there are different things than product IDs stored.

除非您没有显示整个文件,并且存储的产品ID与产品ID不同。

#2


1  

 InputStreamReader converter = new InputStreamReader(System.in);
 BufferedReader in = new BufferedReader(converter);
 while(CurLine = in.readLine())

 //after exiting the loop 

you can use StringBuilder methods "indexOf" and "subString". then you can catch the last id.

你可以使用StringBuilder方法“indexOf”和“subString”。然后你可以抓住最后一个id。

#3


1  

LineNumberReader lnr = null;
try {
    File file = new File("productList.txt");
    lnr = new LineNumberReader(new FileReader(file));
    lnr.skip(Long.MAX_VALUE);
    int lineNumber = lnr.getLineNumber();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
    String productName = getProductName(); // may be a user input
    out.println("Product ID = \""+lineNumber+"\", Product Name = \""+productName+"\"\n");
    out.close();
} catch (Exception ex) {
    // handle it
} finally {
    try {
        lnr.close();
    } catch (IOException ex) {
        Logger.getLogger(H.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I've assumed each line contains a product and sequence number start with 1.

我假设每行包含一个产品,序列号以1开头。

#1


2  

String filename = "test.txt";
int numOfIds = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
    String line = null;
    while ((line = br.readLine()) != null) {
        if (line.contains("Product ID = ")) {
            numOfIds++;
        }
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Should be a good starting point. Save up the last read ID and then use that for appending.

应该是一个很好的起点。保存最后读取的ID,然后使用它进行追加。

Edit: After looking at your question again, I'm not even sure why your .txt file has Product ID =in it. Make the txt file look like this for much easier handling:

编辑:再次查看您的问题后,我甚至不确定您的.txt文件中是否包含产品ID =。使txt文件看起来像这样更容易处理:

001,P1
002,P2
003,P3

unless you didn't show the entire file and there are different things than product IDs stored.

除非您没有显示整个文件,并且存储的产品ID与产品ID不同。

#2


1  

 InputStreamReader converter = new InputStreamReader(System.in);
 BufferedReader in = new BufferedReader(converter);
 while(CurLine = in.readLine())

 //after exiting the loop 

you can use StringBuilder methods "indexOf" and "subString". then you can catch the last id.

你可以使用StringBuilder方法“indexOf”和“subString”。然后你可以抓住最后一个id。

#3


1  

LineNumberReader lnr = null;
try {
    File file = new File("productList.txt");
    lnr = new LineNumberReader(new FileReader(file));
    lnr.skip(Long.MAX_VALUE);
    int lineNumber = lnr.getLineNumber();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
    String productName = getProductName(); // may be a user input
    out.println("Product ID = \""+lineNumber+"\", Product Name = \""+productName+"\"\n");
    out.close();
} catch (Exception ex) {
    // handle it
} finally {
    try {
        lnr.close();
    } catch (IOException ex) {
        Logger.getLogger(H.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I've assumed each line contains a product and sequence number start with 1.

我假设每行包含一个产品,序列号以1开头。