打开文件,发现会是乱码,主要是因为字节数据
package com.sun.print;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WritePrime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
writePrime();
}
/*static void readPrimes() {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("d://prime.txt"));
try {
while (true) {
int val = dis.readInt();
System.out.println(val);
}
} catch (EOFException ex) {
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
*/
static void writePrime() {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("d://aa.txt"));
for (int i = 1; i <= 1000; i++) {
if (isPrime(i))
dos.writeInt(i);
}
System.out.println("write over !");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static boolean isPrime(int n) {
// TODO Auto-generated method stub
for (int i = 2; i <= n / 2; i++)
if (n % i == 0)
return false;
return true;
}
}