1
我做了這個作業練習,從文本文件中讀取文本並將其反轉存儲到另一個新文件中。這是代碼:當使用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 {
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();
}
}
,這裏是一個圖片Picture here
的問題是:爲什麼是新生成的文件大小盡管具有相同的字符,但逆轉下降?
如果有人能解釋它,那將是非常好的,因爲即使是我的教授也不知道爲什麼。
P.S;該文件的上下文只是字典中的一些單詞。 也在其他學生的計算機,所以問題不是從我的電腦
這是爲什麼?換句話說,什麼結果是莫名其妙的?你是否有機會在Windows上運行這個程序,並且計算了原始文件的'\ n \ r'和你寫的'\ n'之間的區別? –
所以你寫'brabuhr.txt'並閱讀'words.txt'?對於將來的開發,我會建議相應地命名這些變量,因爲這樣可以更快地理解代碼。 – Thomas
@ M.leRutte文件大小不同。 – Oleg