我正在尝试将?
替换为我的文本文档中的-
,但只有ArrayList<String>
正在新文件中写入而没有旧版本的所有行。我该如何解决这个问题?
File file = new File("D:\\hl_sv\\L09MF.txt");
ArrayList<String> lns = new ArrayList<String>();
Scanner scanner;
try {
scanner = new Scanner(file);
int lineNum = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
if (line.contains("?")) {
line = line.replace("?", "-");
lns.add(line);
// System.out.println("I found it on line " + lineNum);
}
}
lines.clear();
lines = lns;
System.out.println("Test: " + lines);
FileWriter writer;
try {
writer = new FileWriter("D:\\hl_sv\\L09MF2.txt");
for (String str : lines) {
writer.write(str);
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:3)
我不明白你为什么要在----s
开始存储lines
。我会在阅读时进行变换和打印。您不需要测试List
的存在(如果不存在,则替换赢得的任何内容)。而且,我也会使用try-with-resources
。像
?
答案 1 :(得分:1)
检查此代码:
if (line.contains("?")) {
line = line.replace("?", "-");
lns.add(line);
}
如果它有一个?你只是添加当前行(有替换)?在其中,忽略其他线条。重组它以始终添加现有行。
if (line.contains("?")) {
line = line.replace("?", "-");
}
lns.add(line);
此外,部分
if (line.contains("?"))
扫描line
以查找?,然后搜索代码
line.replace("?", "-");
做同样的事情,但这次也取代了吗?与 - 。您也可以只扫描line
一次:
lns.add(line.replace("?", "-"));
请注意,如果文件很大,仅仅为了保存新行而创建一个ArrayList会浪费相当多的内存。更好的模式是在读取相应的行之后立即写入每行,并在必要时进行修改。
答案 2 :(得分:0)
在while循环中,你有一个if语句检查将更改后的行添加到数组的行。您还需要将未经修改的行添加到数组中。
答案 3 :(得分:0)
这可以解决您的问题:
int lineNum = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
if (line.contains("?")) {
line = line.replace("?", "-");
lns.add(line);
// System.out.println("I found it on line " + lineNum);
}
else{
lns.add(line);
}
以前,如果它包含&#34;?&#34;那么您只是将该行添加到ArrayList中。字符。您需要将该行添加到ArrayList中,无论它是否包含&#34;?&#34;
答案 4 :(得分:0)
如果我尝试使用您想要实现的功能,我会使用不同的方法,请检查此方法并告诉我这是否有助于您:)
public void saveReplacedFile() {
//1. Given a file in your system
File file = new File("D:\\hl_sv\\L09MF.txt");
try {
//2. I will read it, not necessarily with Scanner, but use a BufferedReader instead
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
//3. Define a variable that will hold the value of each line
String line = null;
//and also the information of your file
StringBuilder contentHolder = new StringBuilder();
//why not get your line separator based on your O.S?
String lineSeparator = System.getProperty("line.separator");
//4. Check your file line by line
while ((line = bufferedReader.readLine()) != null) {
contentHolder.append(line);
contentHolder.append(lineSeparator);
}
//5. By this point, your contentHolder will contain all the data of your text
//But it is still a StringBuilder type object, why not convert it to a String?
String contentAsString = contentHolder.toString();
//6. Now we can replace your "?" with "-"
String replacedString = contentAsString.replace("?", "-");
//7. Now, let's save it in a new file using BufferedWriter :)
File fileToBeSaved = new File("D:\\hl_sv\\L09MF2.txt");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileToBeSaved));
bufferedWriter.write(replacedString);
//Done :)
} catch (FileNotFoundException e) {
// Exception thrown if the file does not exist in your system
e.printStackTrace();
} catch (IOException e) {
// Exception thrown due to an issue with IO
e.printStackTrace();
}
}
希望这有帮助。快乐的编码:)
答案 5 :(得分:0)
如果您可以使用Java 8,那么您的代码可以简化为
try (PrintStream ps = new PrintStream("D:\\hl_sv\\L09MF2.txt");
Stream<String> stream = Files.lines(Paths.get("D:\\hl_sv\\L09MF.txt"))) {
stream.map(line -> line.replace('?', '-')).forEach(ps::println);
} catch (IOException e) {
e.printStackTrace();
}