16

I have been trying to write an array to a file. I know how to write integers or String to a file but to bring an array confuses me. I am using this right now:

public static void write (String file, int[]x) throws IOException{
    BufferedWriter outputWriter = null;
    outputWriter = new BufferedWriter(new FileWriter(filename));
    outputWriter.write("hi");// Here I know i cant just write x[0] or anything. Do i need 
                             //to loop in order to write the array?
    outputWriter.newLine();
    outputWriter.flush();  
    outputWriter.close();  



}
Lock1618
  • 199
  • 2
  • 3
  • 9

6 Answers6

22

Like others said, you can just loop over the array and print out the elements one by one. To make the output show up as numbers instead of "letters and symbols" you were seeing, you need to convert each element to a string. So your code becomes something like this:

public static void write (String filename, int[]x) throws IOException{
  BufferedWriter outputWriter = null;
  outputWriter = new BufferedWriter(new FileWriter(filename));
  for (int i = 0; i < x.length; i++) {
    // Maybe:
    outputWriter.write(x[i]+"");
    // Or:
    outputWriter.write(Integer.toString(x[i]);
    outputWriter.newLine();
  }
  outputWriter.flush();  
  outputWriter.close();  
}

If you just want to print out the array like [1, 2, 3, ....], you can replace the loop with this one liner:

outputWriter.write(Arrays.toString(x));
Windle
  • 1,385
  • 2
  • 14
  • 33
20

You can use the ObjectOutputStream class to write objects to an underlying stream.

outputStream = new ObjectOutputStream(new FileOutputStream(filename));
outputStream.writeObject(x);

And read the Object back like -

inputStream = new ObjectInputStream(new FileInputStream(filename));
x = (int[])inputStream.readObject()
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
4

If you're okay with Apache commons lib

outputWriter.write(ArrayUtils.join(array, ","));
djechlin
  • 59,258
  • 35
  • 162
  • 290
3

Just loop over the elements in your array.

Ex:

for(int i=0; numOfElements > i; i++)
{
outputWriter.write(array[i]);
}
//finish up down here
Max
  • 5,799
  • 3
  • 20
  • 27
  • When I use this I get no output to the file. – Lock1618 Dec 04 '12 at 16:30
  • @Lock1618 Are you sure that numOfElements is being set to the length of your array? Or that your array is even populated in the first place? – Max Dec 04 '12 at 16:32
  • The array is just a parameter and i populate it in main. I used for(int i=0; x.length > i; i++) outputWriter.write(x[i]); – Lock1618 Dec 04 '12 at 16:33
  • There's no reason it shouldn't work. Set a couple breakpoints and check it out from there. – Max Dec 04 '12 at 16:36
  • Yea I am not sure it just makes a new file but does not display anything – Lock1618 Dec 04 '12 at 16:38
  • What is the name of the file to which the data is written? Where is it mentioned in the code? – Steven Jul 10 '19 at 17:13
2
private static void saveArrayToFile(String fileName, int[] array) throws IOException {
    Files.write( // write to file
        Paths.get(fileName), // get path from file
        Collections.singleton(Arrays.toString(array)), // transform array to collection using singleton
        Charset.forName("UTF-8") // formatting
    );
}
Zhurov Konstantin
  • 712
  • 1
  • 8
  • 14
0

If the result is for humans to read and the elements of the array have a proper toString() defined...

outputString.write(Arrays.toString(array));
Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70