gpt4 book ai didi

java - 从文件中删除数组项

转载 作者:行者123 更新时间:2023-12-02 03:40:11 25 4
gpt4 key购买 nike

假设我们有一个数组项列表

例如:- a、b、c、d

需要从充满数据的文件中删除,我遗漏了一些东西,任何人都可以帮助我实现这一目标,提前致谢

public static void rmvFromXML(String strFilePath,
String strTmpFilePath) throws IOException {

String currentLine = "";
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

BufferedReader reader = new BufferedReader(new FileReader(strFilePath));
BufferedWriter fileWriter = null;

fileWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(strTmpFilePath)));

while ((currentLine = reader.readLine()) != null) {

String trimmedLine = currentLine.trim();
System.out.println("Trimmed Line :- " + trimmedLine);

for (String value : list) {

System.out.println("Array Value:- " + value);
if (trimmedLine.equals(value))

continue;

fileWriter.write(trimmedLine + System.getProperty("line.separator"));
}
}
fileWriter.close();
reader.close();
}

最佳答案

您可以使用正则表达式。这样您就不必迭代并检查是否行包含您的字符串

while ((currentLine = reader.readLine()) != null) {
Pattern removeWords = Pattern.compile("\\b(?:a|b|c|d)\\b\\s*", Pattern.CASE_INSENSITIVE);
Matcher fix = removeWords.matcher(currentLine);
String fixedString = fix.replaceAll("");
}

尝试用上面的方法来做

input :
abcdefg

output
efg

关于java - 从文件中删除数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com