将整数数组写入文件

时间:2014-12-01 11:56:38

标签: java arrays bufferedwriter

我创建了一个小文件来生成一个指定长度的随机整数数组。我的问题是我很难将文件写入txt文件,然后将其用作排序算法中的参数。错误发生在bufferedWriter中。

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Random;

    public static void main(String[] args) {
        GenerateRandomArray randomizer = new GenerateRandomArray();
// Generate an array of size 10 from -31 to 0.
        int[] x= randomizer.generator(10,-31,0);

        BufferedWriter outputWriter = null;
        outputWriter = new BufferedWriter(new FileWriter(10.txt));
        for (int i = 0; i < x.length; i++) {
        outputWriter.write(Integer.toString(x[i]));
        outputWriter.newLine();
      }
        outputWriter.flush();  
        outputWriter.close();

        System.out.print(Arrays.toString(x));        

    }
    }

1 个答案:

答案 0 :(得分:0)

您需要在coonstructor

中传递File对象或带有文件名的String
outputWriter = new BufferedWriter(new FileWriter("10.txt"));