我的代码有什么问题,尝试保存更改后的 txt 文件,但没有任何反应
WHat is wrong with my code, try to save the changed txt file, but nothing happens
嘿朋友们尝试替换我数据库中的一些逗号,有人可以给我建议我的代码有什么问题吗,已经完成了所有替换,但是没有任何反应
try {
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
String zeile; try {
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff")
.replaceAll(",", "")
.replaceAll("\uffff", "\",\"");
System.out.println(zeile);
}
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(zeile);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
试试这个:
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
String zeile;
try {
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "").replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.write(zeile);
}
fw.close();
}
之前你在做什么,你读取文件直到最后一行并且只保存最后一行而不是整个文件文本。
你写 null
是因为你在 while
循环之后才写(当 zeile
是 null
时终止)。此外,您应该在 finally 块中关闭所有内容(或使用 try-with-resources
。此外,我建议您使用 PrintWriter
以便匹配您的 System.out.println()
(s)。类似的东西,
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
try (BufferedReader br = new BufferedReader(new FileReader(
"C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
PrintWriter fw = new PrintWriter(newTextFile)) {
String zeile;
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "")
.replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.println(zeile);
}
} catch (Exception e) {
e.printStackTrace();
}
你在从循环中出来后写入字符串,因此字符串应该为空,试试这个代码
try {
BufferedReader br = new BufferedReader(
new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
String zeile;
try {
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",",
"").replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.write(zeile);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
嘿朋友们尝试替换我数据库中的一些逗号,有人可以给我建议我的代码有什么问题吗,已经完成了所有替换,但是没有任何反应
try {
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
String zeile; try {
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff")
.replaceAll(",", "")
.replaceAll("\uffff", "\",\"");
System.out.println(zeile);
}
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(zeile);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
试试这个:
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
String zeile;
try {
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "").replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.write(zeile);
}
fw.close();
}
之前你在做什么,你读取文件直到最后一行并且只保存最后一行而不是整个文件文本。
你写 null
是因为你在 while
循环之后才写(当 zeile
是 null
时终止)。此外,您应该在 finally 块中关闭所有内容(或使用 try-with-resources
。此外,我建议您使用 PrintWriter
以便匹配您的 System.out.println()
(s)。类似的东西,
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
try (BufferedReader br = new BufferedReader(new FileReader(
"C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
PrintWriter fw = new PrintWriter(newTextFile)) {
String zeile;
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "")
.replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.println(zeile);
}
} catch (Exception e) {
e.printStackTrace();
}
你在从循环中出来后写入字符串,因此字符串应该为空,试试这个代码
try {
BufferedReader br = new BufferedReader(
new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
String zeile;
try {
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",",
"").replaceAll("\uffff", "\",\"");
System.out.println(zeile);
fw.write(zeile);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}