Java-打开txt文件并清除所有多个空格
我有一个txt文件,我试图做的是打开它,删除所有多个空间,使它们成为唯一的一个。我使用:Java-打开txt文件并清除所有多个空格,java,bufferedreader,bufferedwriter,Java,Bufferedreader,Bufferedwriter,我有一个txt文件,我试图做的是打开它,删除所有多个空间,使它们成为唯一的一个。我使用: br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt")); bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two.txt")); while ((current_line = br.readLine()
br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt"));
bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two.txt"));
while ((current_line = br.readLine()) != null) {
//System.out.println("Here.");
current_line = current_line.replaceAll("\\s+", " ");
bw.write(current_line);
}
br.close();
bw.close();
然而,至少在我看来是正确的,文件上没有写任何东西。如果我使用system.out.println命令,它不会被打印,这意味着执行永远不会在while循环中。。。我做错了什么?谢谢您正在阅读文件,同时在上面写入内容。这是不允许的 因此,最好的方法是先读取文件并将处理后的文本存储在另一个文件中,最后用新文件替换原始文件..试试这个
br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt"));
bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two_copy.txt"));
String current_line;
while ((current_line = br.readLine()) != null) {
//System.out.println("Here.");
current_line = current_line.replaceAll("\\s+", " ");
bw.write(current_line);
bw.newLine();
}
br.close();
bw.close();
File copyFile = new File("C:\\Users\\Chris\\Desktop\\file_two_copy.txt");
File originalFile = new File("C:\\Users\\Chris\\Desktop\\file_two.txt");
originalFile.delete();
copyFile.renameTo(originalFile);
这可能会有帮助…您正在读取文件,同时在文件上写入内容。这是不允许的 因此,最好的方法是先读取文件并将处理后的文本存储在另一个文件中,最后用新文件替换原始文件..试试这个
br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt"));
bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two_copy.txt"));
String current_line;
while ((current_line = br.readLine()) != null) {
//System.out.println("Here.");
current_line = current_line.replaceAll("\\s+", " ");
bw.write(current_line);
bw.newLine();
}
br.close();
bw.close();
File copyFile = new File("C:\\Users\\Chris\\Desktop\\file_two_copy.txt");
File originalFile = new File("C:\\Users\\Chris\\Desktop\\file_two.txt");
originalFile.delete();
copyFile.renameTo(originalFile);
这可能会有帮助…您必须先读后写,不允许同时读写同一个文件,您需要使用
RandomAccessFile
如果您不想学习新技术,则需要写入单独的文件,或将所有行缓存到内存(即ArrayList
),但必须在初始化BufferedWriter
之前关闭BufferedReader
,否则会出现文件访问错误
编辑:
如果您想查看它,下面是一个RandomAccessFile
用例示例,用于您的预期用途。值得指出的是,这仅在最终行长度小于或等于原始行长度时才起作用,因为此技术基本上覆盖了现有文本,但速度非常快,内存开销很小,并且适用于超大文件:
public static void readWrite(File file) throws IOException{
RandomAccessFile raf = new RandomAccessFile(file, "rw");
String newLine = System.getProperty("line.separator");
String line = null;
int write_pos = 0;
while((line = raf.readLine()) != null){
line = line.replaceAll("\\s+", " ") + newLine;
byte[] bytes = line.getBytes();
long read_pos = raf.getFilePointer();
raf.seek(write_pos);
raf.write(bytes, 0, bytes.length);
write_pos += bytes.length;
raf.seek(read_pos);
}
raf.setLength(write_pos);
raf.close();
}
必须先读后写,不允许同时对同一文件进行读写,您需要使用
RandomAccessFile
如果您不想学习新技术,则需要写入单独的文件,或将所有行缓存到内存(即ArrayList
),但必须在初始化BufferedWriter
之前关闭BufferedReader
,否则会出现文件访问错误
编辑:
如果您想查看它,下面是一个RandomAccessFile
用例示例,用于您的预期用途。值得指出的是,这仅在最终行长度小于或等于原始行长度时才起作用,因为此技术基本上覆盖了现有文本,但速度非常快,内存开销很小,并且适用于超大文件:
public static void readWrite(File file) throws IOException{
RandomAccessFile raf = new RandomAccessFile(file, "rw");
String newLine = System.getProperty("line.separator");
String line = null;
int write_pos = 0;
while((line = raf.readLine()) != null){
line = line.replaceAll("\\s+", " ") + newLine;
byte[] bytes = line.getBytes();
long read_pos = raf.getFilePointer();
raf.seek(write_pos);
raf.write(bytes, 0, bytes.length);
write_pos += bytes.length;
raf.seek(read_pos);
}
raf.setLength(write_pos);
raf.close();
}
您的方法几乎没有问题:
- 主要的一点是,您正试图同时读取和写入同一个文件李>
- 另一个是
总是创建新的空文件,这会阻止newfilewriter(..)
从您的文件中读取任何内容FileReader
file1
中的内容,并在file2
中编写其修改版本。然后将file1
替换为file2
您的代码看起来或多或少像
Path input = Paths.get("input.txt");
Path output = Paths.get("output.txt");
List<String> lines = Files.readAllLines(input);
lines.replaceAll(line -> line.replaceAll("\\s+", " "));
Files.write(output, lines);
Files.move(output, input, StandardCopyOption.REPLACE_EXISTING);
Path input=Path.get(“input.txt”);
Path output=Path.get(“output.txt”);
列表行=文件。readAllLines(输入);
行.replaceAll(行->行.replaceAll(“\\s+”,”);
文件。写入(输出,行);
文件。移动(输出、输入、StandardCopyOption。替换现有文件);
您的方法几乎没有问题:
- 主要的一点是,您正试图同时读取和写入同一个文件李>
- 另一个是
总是创建新的空文件,这会阻止newfilewriter(..)
从您的文件中读取任何内容FileReader
file1
中的内容,并在file2
中编写其修改版本。然后将file1
替换为file2
您的代码看起来或多或少像
Path input = Paths.get("input.txt");
Path output = Paths.get("output.txt");
List<String> lines = Files.readAllLines(input);
lines.replaceAll(line -> line.replaceAll("\\s+", " "));
Files.write(output, lines);
Files.move(output, input, StandardCopyOption.REPLACE_EXISTING);
Path input=Path.get(“input.txt”);
Path output=Path.get(“output.txt”);
列表行=文件。readAllLines(输入);
行.replaceAll(行->行.replaceAll(“\\s+”,”);
文件。写入(输出,行);
文件。移动(输出、输入、StandardCopyOption。替换现有文件);
您的代码使用StringReader和Writer而不是文件对我有效,因此循环和关闭很好。您的代码使用StringReader和Writer而不是文件对我有效,因此循环和关闭很好。