import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.io.IOException;
import java.nio.IntBuffer;
public class TryPrimes{
public static void main(String[] args){
int[] primes=new int[100];
int a=0;
boolean isPrimes=true;
Outerloop:
for(int i=3;i<=100;i++){
for(int j=2;j<i;j++){
if(i%j==0){
isPrimes=false;
continue Outerloop;
}
}
primes[a]=i;
System.out.println(primes[a]);
a++;
}
String dirName="f:/stuff";
String fileName="primes.txt";
File dir=new File(dirName);
if(!dir.exists()){
if(!dir.mkdir()){
System.out.println("Can not created directory!");
}
}
File aFile=new File(dirName,fileName);
FileOutputStream outputFile=null;
try{
outputFile=new FileOutputStream(aFile,true);
System.out.println("OutputStream created!");
}catch(FileNotFoundException e){
e.printStackTrace(System.err);
}
FileChannel outChannel=outputFile.getChannel();
ByteBuffer buf=ByteBuffer.allocate(1024);
IntBuffer intBuffer=buf.asIntBuffer();
for(int prime:primes){
intBuffer.put(prime);
}
try{
outChannel.write(buf);
outputFile.close();
System.out.println("contents have been writed!");
}catch(IOException e){
e.printStackTrace(System.err);
}
System.exit(0);
}
}
为什么写进文件的不是素数而是乱码...帮忙看下啊..