How to Write an Array to a File Java

how to write an array to a file Java

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

How to write or store string array in .txt file in java

public void DataSave() {
File fout = new File("Data.txt");
try (FileOutputStream fos = new FileOutputStream(fout); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
for (String s : numberOfProperty) {
bw.write(s);
bw.newLine();
}
} catch (IOException ignored) {

}
}

You need to close the BufferedReader. A better solution is using try-with-resources, so you don't need to worry about closing.

You can have multiple resources in the try-with-resources separated by a ;.


Java 13+, you can use Path.of():

public void DataSave() {
File fout = new File("Data.txt");
try (FileOutputStream fos = new FileOutputStream(fout); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
Files.write(Path.of("Data.txt"), Collections.singletonList(numberOfProperty));
} catch (IOException ignored) {

}
}

You can also write your array in one line as:

String[] numberOfProperty = {"1", "2", "3"};

Writing an array to a file

for(i = 0; ++i < length; i++) {

I don't think you want to increase i twice. Get rid of the ++i.

Store java arrays to file for read and write

In order to store an object (datastructure), you need to serialize it: convert it to an stream of bytes, characters, whatever.

There exist multiple ways to serialize an object ranging from JSON over XML to the object serializer/deserializer.

For instance (as specified in the webpage):

First you must add implements Serializable at the end of the class definition, so:

public class BestTimes implements Serializable {

Then you can serialize using:

try(FileOutputStream f = new FileOutputStream("file.txt");
ObjectOutput s = new ObjectOutputStream(f)) {
s.writeObject(beginner);
s.writeObject(intermediate);
s.writeObject(expert);
}

And later read it as:

BestTimes[] beginner, intermediate, expert;
try(FileInputStream in = new FileInputStream("file.txt");
ObjectInputStream s = new ObjectInputStream(in)) {
beginner = (BestTimes[]) s.readObject();
intermediate = (BestTimes[]) s.readObject();
expert = (BestTimes[]) s.readObject();
}

The easy thing about the object serializer is that you don't have to define a format on your own. That's the task of the Java virtual machine, and furthermore it can encode all kinds of objects whereas JSON for instance can't handle datastructures that contain loops (example: graphs), XML can't handle loops as well and CSV is only well suited for table content.

A disadvantage however is that the datastructures are encoded binary: they cannot be read easily. Although in this case that might be an advantage since some users tend to alter the file to increase their highscores ;).



Related Topics



Leave a reply



Submit