gpt4 book ai didi

java - 如何用java中的其他字符串替换文件中的字符串

转载 作者:行者123 更新时间:2023-11-29 05:04:23 28 4
gpt4 key购买 nike

我有一个文件,每一行都包含这样的字符串:

usual,proper,complete,1,convenient,convenient,nonprob,recommended,recommend

我想用这样的代码替换每个单词:

1000, 100, 110, 110, 111, 001, 111, 111, 1000

这是我使用的代码,但它仍然不完整:

public class Codage {
BufferedReader in;

public Codage() {
try {
in = new BufferedReader(new FileReader("nursery.txt"));
FileOutputStream fos2 = new FileOutputStream("nursery.txt");
DataOutputStream output = new DataOutputStream(fos2);
String str;

while (null != ((str = in.readLine()))) {

String delims = ",";
String[] tokens = str.split(delims);
int tokenCount = tokens.length;
for (int j = 0; j < tokenCount; j++) {
if (tokens[j].equals("usual")) {
tokens[j] = "1000";
output.writeChars(tokens[j]);

}
//continue the other cases
}
System.out.print(str);
}

in.close();

} catch (IOException e) {

System.out.println("There was a problem:" + e);
}
}

public static void main(String[] args) {
Codage c = new Codage();
}

}

我的代码错误地替换了值。

最佳答案

首先,您在此处编写的代码不起作用,因为当您打开一个输出流到您尝试读取的确切文件时,它将清空源文件和语句 in.readLine() 总是返回 null。因此,如果这是您的真实代码,那么这可能就是问题所在。

我假设您知道您应该将打开以读取的文件与要写入的文件分开。也就是说,当您打开要读取的 nursery.txt 时,您应该在同一路径中创建一个名为 nursery.tmp 的临时文件的 outputStream,在该过程完成后,您可以删除 nursery.txt 并重命名 nursery .tmp 到 nursery.txt。

另外,如果我是你,我也不会使用 if-else 结构来完成这项工作。它接缝你有独特的 key ,如:

usual, proper, complete, convenient, convenient, nonprob, recommended, recommend

所以也许使用映射结构来查找替换值更方便:

usual, proper, complete, convenient, convenient, nonprob, recommended, recommend, ...

1000, 100, 110, 110, 111, 001, 111, 111, ...

但这些只是一些猜测,您知道如何管理您的业务逻辑。

在那部分之后,我认为将输出数据创建为字符串行并将它们逐行写入 nursery.tmp 是一个更好的主意:

public class Codage {

private BufferedReader in;
private BufferedWriter out;

private HashMap<String, String> replacingValuesByKeys = new HashMap<String, String>();

public Codage() {
initialize();
}

private void initialize() {
// I assumed that you have rule that a key like "proper" always goes to "100"
// Initialize the map between keys and replacing values:
replacingValuesByKeys.put("usual", "1000");
replacingValuesByKeys.put("proper", "100");
replacingValuesByKeys.put("complete", "110");
replacingValuesByKeys.put("convenient", "110");
replacingValuesByKeys.put("nonprob", "111");
replacingValuesByKeys.put("recommended", "001");
replacingValuesByKeys.put("recommend", "1000");
}

public void doRelpacementInFile(){
try {
in = new BufferedReader(new FileReader("c:/nursery.txt"));
out = new BufferedWriter(new FileWriter("c:/nursery.tmp"));

String str = in.readLine();
while (null != str) {
Iterator<String> it = replacingValuesByKeys.keySet().iterator();
while(it.hasNext())
{
String toBeReplaced = it.next();
String replacementValue = replacingValuesByKeys.get(toBeReplaced);
// \\b is for word boundary, because you have both recommend and recommended
// and we do not want to replacing the [recommend] part of recommended.
str = str.replaceAll("\\b"+toBeReplaced+"\\b", replacementValue);
}
// Write the fully replaced line to the temp file:
out.append(str);
out.newLine();

// Do not forget to read the next line:
str = in.readLine();
}

} catch (IOException e) {
System.out.println("There was a problem:" + e);
} finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}


File f = new File("c:/nursery.txt");
f.delete();

File f2 = new File("c:/nursery.tmp");
f2.renameTo(new File("c:/nursery.txt"));
}


public static void main(String[] args) {
Codage c = new Codage();
c.doRelpacementInFile();
}

}

希望这些片段对您有所帮助,

祝你好运。

关于java - 如何用java中的其他字符串替换文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927980/

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