我正在尝试从文本文件中删除所有逗号,我在哪里出错?我认为这与replaceAll领域有关,尽管我已经对其进行了研究,但找不到任何答案。我还需要在“;”之后加一个新行。以及删除逗号。先感谢您
`public static void open(){
// The name of the file to open.
String fileName = "Test.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
line.replaceAll(",","\\.");
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}`
最佳答案
字符串在Java中是不可变的,因此System.out.println(line.replaceAll(",","\\."))
是您想要的。您要打印返回的值。