📄 primesfile.java
字号:
import java.io.*;
public class PrimesFile
{
RandomAccessFile raf;
public static void main(String args[]) throws IOException
{
(new PrimesFile()). createprime(100);
}
public void createprime(int max) throws IOException
{
raf=new RandomAccessFile("primes.bin","rw");//创建文件对象
raf.seek(0); //文件指针为0
raf.writeInt(2); //写入整型
int k=3;
while (k<=max)
{
if (isPrime(k))
raf.writeInt(k);
k = k+2;
}
output(max);
raf.close(); //关闭文件
}
public boolean isPrime(int k) throws IOException
{
int i=0,j;
boolean yes = true;
try
{
raf.seek(0);
int count = (int)(raf.length()/4); //返回文件字节长度
while ((i<=count) && yes)
{
if (k % raf.readInt()==0) //读取整型
yes = false;
else
i++;
raf.seek(i*4); //移动文件指针
}
} catch(EOFException e) { } //捕获到达文件尾异常
return yes;
}
public void output(int max) throws IOException
{
try
{
raf.seek(0);
System.out.println("[2.."+max+"]中有 "+
(raf.length()/4)+" 个素数:");
for (int i=0;i<(int)(raf.length()/4);i++)
{
raf.seek(i*4);
System.out.print(raf.readInt()+" ");
if ((i+1)%10==0) System.out.println();
}
} catch(EOFException e) { }
System.out.println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -