Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write an array to a file Java

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



}
like image 930
Lock1618 Avatar asked Dec 04 '12 16:12

Lock1618


People also ask

Can you write an array to a file in Java?

You can use write data into a file using the Writer classes. In the example given below, we are writing the contents of the array using the BufferedWriter.

How do I write an array of data into a file?

Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode: FILE *f = fopen("client. data", "wb"); fwrite(clientdata, sizeof(char), sizeof(clientdata), f); fclose(f);

How do you create an array of files in Java?

File> files = new ArrayList<>(); and add the files like: files. add(file);

How to write an ArrayList to a file in Java?

Learn the easy way to write an ArrayList values to a file in Java - with code examples You can write an ArrayList object values to a plain text file in Java by using the built-in java.nio.file package.

How to write into a file in Java?

There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows: This method is supported by Java version 11. This method can take four parameters. These are file path, character sequence, charset, and options.

How to create an array in Java?

Java Arrays. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets: String[] cars; We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the ...

How to store content of a file in an array in Java?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method. To store the content of the file better use the collection storage type instead of a static array as we don’t know the exact lines or word count of files.


2 Answers

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));
like image 62
Windle Avatar answered Sep 16 '22 23:09

Windle


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()
like image 37
Subhrajyoti Majumder Avatar answered Sep 18 '22 23:09

Subhrajyoti Majumder