how to write an array to a file Java

116,088

Solution 1

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));

Solution 2

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()

Solution 3

If you're okay with Apache commons lib

outputWriter.write(ArrayUtils.join(array, ","));

Solution 4

Just loop over the elements in your array.

Ex:

for(int i=0; numOfElements > i; i++)
{
outputWriter.write(array[i]);
}
//finish up down here

Solution 5

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
    );
}
Share:
116,088
Lock1618
Author by

Lock1618

Updated on September 24, 2020

Comments

  • Lock1618
    Lock1618 over 3 years

    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
    Lock1618 over 11 years
    Thankyou but when i use this i get a bunch of random number letters and symbols
  • Subhrajyoti Majumder
    Subhrajyoti Majumder over 11 years
    @Lock1618 read object back like - new ObjectInputStream(new FileInputStream(filename)).readObject();
  • Lock1618
    Lock1618 over 11 years
    When I use this I get no output to the file.
  • Max
    Max over 11 years
    @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?
  • Lock1618
    Lock1618 over 11 years
    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]);
  • Max
    Max over 11 years
    There's no reason it shouldn't work. Set a couple breakpoints and check it out from there.
  • Lock1618
    Lock1618 over 11 years
    Yea I am not sure it just makes a new file but does not display anything
  • Steven
    Steven almost 5 years
    What is the name of the file to which the data is written? Where is it mentioned in the code?