package 文件操作;
import java.io.FileOutputStream;
import java.io.IOException;
public class ss {
ss()
{
try
{
FileOutputStream f=new FileOutputStream("1.txt");
int i,j,k;
for(i=1;i<100;i++)
{
boolean flag=true;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=false;
break;
}
}
if(flag)
{
f.write(i);
}
}
f.flush();
f.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args){
new ss();
}
}
//sr.java文件
package 文件操作;
import java.io.FileInputStream;
import java.io.IOException;
public class sr {
sr(){
try{
FileInputStream w=new FileInputStream("1.txt");
int l=0,c;
while((c=w.read())!=-1){
System.out.print(c+" ");
l++;
}
System.out.print("1-100的素數***有:"+l);
w.close();
}catch(IOException w)
{
w.printStackTrace();
}
}
public static void main(String[] args) {
new sr();
}
}
運行結果:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 1-100的素數***有:26