Write a program and write a static method that randomly generates 200 three-digit integers, finds the prime numbers among them, and saves them to the file prime.txt.

topic

Write a program and write a static method that randomly generates 200 three-digit integers, finds the prime numbers among them, and saves them to the file prime.txt. Then write another static method to read these prime numbers from the file and output these prime numbers in the form of 10 per line. Test it in the main method.
Tip: It is recommended to use the classes DataOutputStream, DataInputStream, FileInputStream, FileOutputStream

package comprehensive_test.test2;

import java.io.*;

/**
 * @Auther: 茶凡
 * @ClassName Prime
 * @Description TODO
 * @date 2023/6/8 15:15
 * @Version 1.0
 */
public class Prime {
    
    
    public static void main(String[] args) {
    
    
        generateRandom();
        findPrime();
    }


    public static void generateRandom(){
    
    
        File file = new File("src\\comprehensive_test\\test2\\random.txt");
        try {
    
    
            FileOutputStream fos = new FileOutputStream(file);

            DataOutputStream dos = new DataOutputStream(fos);

            for (int i = 0; i <200 ; i++) {
    
    
                int num = (int) (Math.random()*100) + 100;
                //System.out.println(num);
                dos.writeInt (num);
               // dos.writeUTF("\n");
            }
            // 关闭流
            dos.close();
            fos.close();
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

    }

    public static void findPrime(){
    
    
        File file = new File("src\\comprehensive_test\\test2\\random.txt");
        File file1 = new File("src\\comprehensive_test\\test2\\prime.txt");
        try {
    
    
            FileInputStream fis = new FileInputStream(file);
            DataInputStream dis = new DataInputStream(fis);

            FileOutputStream fos = new FileOutputStream(file1);
            DataOutputStream dos = new DataOutputStream(fos);

            while (dis.available()>0){
    
    
                int i = dis.readInt();
                if(isPrime(i)){
    
    
                    dos.writeInt(i);
                }

            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

    }

    public static boolean isPrime(int num) {
    
    
        // 如果 num 小于等于 1 则不是素数
        if (num <= 1) {
    
    
            return false;
        }

        // 如果 num 等于 2,则是素数
        if (num == 2) {
    
    
            return true;
        }

        // 如果 num 是偶数,则不是素数
        if (num % 2 == 0) {
    
    
            return false;
        }

        // 判断 num 是否能被奇数整除
        for (int i = 3; i <= Math.sqrt(num); i += 2) {
    
    
            if (num % i == 0) {
    
    
                return false;
            }
        }
        // 如果上述条件都不满足,则 num 是素数
        return true;
    }

}

Guess you like

Origin blog.csdn.net/weixin_45833112/article/details/131155628