当使用BufferedWriter和FileWriter时,如果要正确地将整数写入"caseCountFile",需要进行以下步骤:
FileWriter fileWriter = new FileWriter("caseCountFile.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
int caseCount = 10;
String caseCountStr = String.valueOf(caseCount);
bufferedWriter.write(caseCountStr);
bufferedWriter.close();
完整的代码示例如下:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
FileWriter fileWriter = new FileWriter("caseCountFile.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
int caseCount = 10;
String caseCountStr = String.valueOf(caseCount);
bufferedWriter.write(caseCountStr);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样,整数就会正确地写入"caseCountFile.txt"文件中。
领取专属 10元无门槛券
手把手带您无忧上云