不能用 FileWriter 写数字

Can't write number with FileWriter

这是我的代码,我希望将 b[i] 写入文件名 test.txt,但它不起作用。它只是写一些类似符号的东西。感谢您的帮助。

public class btl {
public static boolean IsPrime(int n) {

    if (n < 2) {
        return false;
    }

    int squareRoot = (int) Math.sqrt(n);
    for (int i = 2; i <= squareRoot; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}




public static void main(String args[]) {
    Scanner scanIn = new Scanner(System.in);
    int first = 0;
    int second = 0;
    try {
        System.out.println("Input First Number");
     first = scanIn.nextInt();
        System.out.println("Input Second Number");
        second= scanIn.nextInt();
    }
    catch(Exception e) {
        System.out.println("Something wrong!");
    }
    int x = first;
    int y = second;
    int a;
    int[] b = new int[y];

    Thread threadA = new Thread(new Runnable() {
        @Override
        public void run() {     
            int c=0;
            for(int i=x; i<y; i++) {   
                if(IsPrime(i)) {
                    b[c] = i;
                    System.out.println(b[c]);
                    c++;
               }
         }


        }
    });
    threadA.start();


    try(FileWriter fw = new FileWriter("test.txt")){

            for(int i =0; i<y; i++)
            {
             fw.write(b[i]);
            }
        }
    catch(IOException exc) {
        System.out.println("EROR! " + exc);
    }

}

}

This is my display console

And this is file "test.txt"

我不知道为什么FileWriter 会在文件text.txt 中写入符号。我想要它的数组 b[i].

FileWriter.write(int) 写入 单个字符 。 你可能想要 FileWriter.write(String) 如:

    fw.write(Integer.toString(b[i]));