使用 try-with-resources 读写同一个文件
Reading and writing to the same file using try-with-resources
我正在尝试制作一个程序来接收指定的 String
,并删除文本文档中出现的每个 String
。用于读/写的文本文件是相同的。使用的参数是从 cmd 接收的,顺序为:
inputString filename
程序编译正常,但在 运行 之后,原始文本文件留空。如果我制作一个用于输入处理的 try-catch 块和一个用于输出处理的 try-catch 块,我就能够读取和写入同一个文件。如果我使用 try-with-resources 块,我能够读取一个文件,并将输出保存到与原始文件不同的文件中,并且从 cmd 中删除所有出现的 inputString
。但似乎我无法使用 try-with-resources 读写同一个文件,而且当我尝试这样做时 input.hasNext()
语句 returns false。
下面的代码示例:
package ch12;
import java.io.*;
import java.util.*;
public class Chapter_12_E11_RemoveText {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage java ch12.Chapter_12_E11_RemoveText inputString filename");
System.exit(1);
}
File filename = new File(args[1]);
if (!filename.exists()) {
System.out.println("Target file " + args[1] + " does not exist");
System.exit(2);
}
try (
Scanner input = new Scanner(filename);
PrintWriter output = new PrintWriter(filename);
) {
System.out.println("hasNext() is " + input.hasNext());
System.out.println("hasNextLine() is " + input.hasNextLine());
while (input.hasNext()) {
String s1 = input.nextLine();
System.out.println("String fetched from input.nextLine() " + s1);
System.out.println("Attemping to replace all words equal to " + args[0] + " with \"\"");
String s2 = s1.replaceAll(args[0], "");
output.println(s2);
}
}
}
}
我怀疑当我使用参数 filename
创建一个新的 PrintWriter
对象时,原始文件在 while-loop
执行之前被空白文件覆盖。我在这儿吗?是否可以使用 try-with-resources 读写同一个文件?
来自 PrintWriter docs:
If the file exists then it will be truncated to zero size; otherwise, a new file will be created.
所以你是对的,当你初始化你的 PrintWriter
时,你的 Scanner
没有任何东西可以扫描。
我会从资源初始化块中删除 PrintWriter
初始化,在内存中构建文件表示,然后将文件内容替换到另一个块中(或将其嵌套)。
也就是说,如果文件的大小适合您的内存来处理替换。
我正在尝试制作一个程序来接收指定的 String
,并删除文本文档中出现的每个 String
。用于读/写的文本文件是相同的。使用的参数是从 cmd 接收的,顺序为:
inputString filename
程序编译正常,但在 运行 之后,原始文本文件留空。如果我制作一个用于输入处理的 try-catch 块和一个用于输出处理的 try-catch 块,我就能够读取和写入同一个文件。如果我使用 try-with-resources 块,我能够读取一个文件,并将输出保存到与原始文件不同的文件中,并且从 cmd 中删除所有出现的 inputString
。但似乎我无法使用 try-with-resources 读写同一个文件,而且当我尝试这样做时 input.hasNext()
语句 returns false。
下面的代码示例:
package ch12;
import java.io.*;
import java.util.*;
public class Chapter_12_E11_RemoveText {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage java ch12.Chapter_12_E11_RemoveText inputString filename");
System.exit(1);
}
File filename = new File(args[1]);
if (!filename.exists()) {
System.out.println("Target file " + args[1] + " does not exist");
System.exit(2);
}
try (
Scanner input = new Scanner(filename);
PrintWriter output = new PrintWriter(filename);
) {
System.out.println("hasNext() is " + input.hasNext());
System.out.println("hasNextLine() is " + input.hasNextLine());
while (input.hasNext()) {
String s1 = input.nextLine();
System.out.println("String fetched from input.nextLine() " + s1);
System.out.println("Attemping to replace all words equal to " + args[0] + " with \"\"");
String s2 = s1.replaceAll(args[0], "");
output.println(s2);
}
}
}
}
我怀疑当我使用参数 filename
创建一个新的 PrintWriter
对象时,原始文件在 while-loop
执行之前被空白文件覆盖。我在这儿吗?是否可以使用 try-with-resources 读写同一个文件?
来自 PrintWriter docs:
If the file exists then it will be truncated to zero size; otherwise, a new file will be created.
所以你是对的,当你初始化你的 PrintWriter
时,你的 Scanner
没有任何东西可以扫描。
我会从资源初始化块中删除 PrintWriter
初始化,在内存中构建文件表示,然后将文件内容替换到另一个块中(或将其嵌套)。
也就是说,如果文件的大小适合您的内存来处理替换。