我正在編寫一個代碼,用於檢查數字是否爲素數,如果是,則將其打印在文件中,但是當我打開InputStream時,它會自動刪除我的文件中的內容! 這裏是我的代碼在java中讀取文件時刪除的內容
import java.util.Scanner;
import java.io.*;
@SuppressWarnings("all")
public class PrimeList {
public static void main(String[]args) throws NumberFormatException, IOException {
String name = "C:\\Text\\primelist.txt";
File file = new File(name);
try {
OutputStreamWriter wr = new OutputStreamWriter(new FileOutputStream(file));
BigInteger bi = BigInteger.valueOf(5);
BigInteger zero = BigInteger.ZERO;
BigInteger two = BigInteger.valueOf(2);
while(true) {
BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while(true) {
BigInteger prime = BigInteger.valueOf(Long.parseLong(read.readLine()));
誰能告訴我如何解決這一問題?
請顯示您的程序的完整代碼。 – Divers
您正在讀取和寫入同一個文件...首先閱讀文件...然後創建新文件並將結果追加到該文件中。 –