在使用Java将文本从一个文件反转和存储到另一个文件时减小大小
我做了这个家庭作业练习,从一个文本文件中读取文本并将其反向存储到另一个新文件中。代码如下:在使用Java将文本从一个文件反转和存储到另一个文件时减小大小,java,Java,我做了这个家庭作业练习,从一个文本文件中读取文本并将其反向存储到另一个新文件中。代码如下: import java.util.*; import java.io.*; public class FileEcho { File file; Scanner scanner; String filename = "words.txt"; File file1 ; PrintWriter pw ; void echo() { try { Strin
import java.util.*;
import java.io.*;
public class FileEcho {
File file;
Scanner scanner;
String filename = "words.txt";
File file1 ;
PrintWriter pw ;
void echo() {
try {
String line;
file = new File( filename);
scanner = new Scanner( file );
file1 = new File("brabuhr.txt");
pw = new PrintWriter(file1);
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String s = new StringBuilder(line).reverse().toString();
pw.println(s);
}
scanner.close();
} catch(FileNotFoundException e) {
System.out.println( "Could not find or open file <"+filename+">\n"+e
);
}
}
public static void main(String[] args) {
new FileEcho().echo();
}
}
import java.util.*;
导入java.io.*;
公共类FileEcho{
文件;
扫描仪;
字符串filename=“words.txt”;
文件文件1;
印刷作家;
void echo(){
试一试{
弦线;
文件=新文件(文件名);
扫描仪=新扫描仪(文件);
file1=新文件(“brabuhr.txt”);
pw=新的PrintWriter(文件1);
while(scanner.hasNextLine()){
line=scanner.nextLine();
字符串s=新的StringBuilder(line).reverse().toString();
pw.println(s);
}
scanner.close();
}catch(filenotfounde异常){
System.out.println(“无法找到或打开文件\n”+e
);
}
}
公共静态void main(字符串[]args){
新文件echo().echo();
}
}
这是一张照片
问题是:为什么新生成的文件虽然具有相同的字符,但大小减小了?
如果有人能解释就好了,因为连我的教授都不知道这是为什么
附则;文件的上下文只是字典中的一些单词。
同样在其他学生的计算机中,因此问题不是来自我的计算机问题是您从未关闭输出流pw,因此任何挂起的输出都不会写入基础文件。这可能会导致文件被截断 您应该在
finally
中或在资源试用中使用pw.close()
关闭输出流
try (pw = new PrintWriter(file1)) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String s = new StringBuilder(line).reverse().toString();
pw.println(s);
}
}
您的实现可以简化为以下内容:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileEcho {
void echo() throws IOException {
try (PrintWriter pw = new PrintWriter("brabuhr.txt")) {
Files.lines(Paths.get("words.txt"))
.map(s -> new StringBuilder(s).reverse().toString())
.forEach(pw::println);
}
}
public static void main(String[] args) throws IOException {
new FileEcho().echo();
}
}
在本例中,我使用了“尝试使用资源”将
PrintWriter pw
自动关闭。问题在于您从未关闭输出流pw
,因此任何挂起的输出都不会写入基础文件。这可能会导致文件被截断
您应该在finally
中或在资源试用中使用pw.close()
关闭输出流
try (pw = new PrintWriter(file1)) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String s = new StringBuilder(line).reverse().toString();
pw.println(s);
}
}
您的实现可以简化为以下内容:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileEcho {
void echo() throws IOException {
try (PrintWriter pw = new PrintWriter("brabuhr.txt")) {
Files.lines(Paths.get("words.txt"))
.map(s -> new StringBuilder(s).reverse().toString())
.forEach(pw::println);
}
}
public static void main(String[] args) throws IOException {
new FileEcho().echo();
}
}
在本例中,我使用了“尝试使用资源”将
PrintWriter pw
自动关闭。这是为什么?换句话说,什么结果如此令人费解?您是否碰巧在Windows上运行了此操作,并计算了原始文件的\n\r
与您编写的\n
之间的差异?因此您正在编写brabuhr.txt
并读取words.txt
?对于未来的开发,我建议相应地命名变量,因为这样可以更容易地快速理解代码。@M.leRutte文件的大小不同。请参阅我的注释编辑。使用hexviewer检查,您将看到差异。这是为什么?换句话说,什么结果如此令人费解?您是否碰巧在Windows上运行了此操作,并计算了原始文件的\n\r
与您编写的\n
之间的差异?因此您正在编写brabuhr.txt
并读取words.txt
?对于未来的开发,我建议相应地命名变量,因为这样可以更容易地快速理解代码。@M.leRutte文件的大小不同。请参阅我的注释编辑。使用hexviewer进行检查,您将看到差异。