我运行这段代码,我得到“文件写入!” 当我打开文件查看它时,所写的每一件事都没有任何意义。你可以理解我想在文件中写012345678910。除了buffW.write(k);
. 我还有其他错误吗?
package thema4_create_write_read_file;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class FW {
public static void main(String[] args) {
File newFile = new File("newFile.txt");
if (newFile.exists()) {
System.out.println("The file already exists");
} else {
try {
newFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileWriter fileW = new FileWriter(newFile);
BufferedWriter buffW = new BufferedWriter(fileW);
for (int k = 0; k <= 10; k++) {
buffW.write(k); // This is where the problem occurs
}
buffW.close();
System.out.print("File written !");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
有没有办法将 (k) 写为整数而不是字符串,以便将其读为 int 呢?