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
How to Rotate Jpeg Images Based on the Orientation Metadata
Implementing Custom Methods of Spring Data Repository and Exposing Them Through Rest
Looking for Java Spell Checker Library
How to Retrieve the Autoincrement Id from a Prepared Statement
How to Use the Unsigned Integer in Java 8 and Java 9
Differencebetween Cascade & Inverse in Hibernate, What Are They Used For
Attach the Source in Eclipse of a Jar
Automatic Reserved Word Escaping for Hibernate Tables and Columns
How to Shutdown an Executorservice
How to Pass an Integer Class Correctly by Reference
Differencebetween Declaration and Definition in Java
Superclass Reference Not Able to Call Subclass Method in Java
Interpreting Jemaloc Data Possible Off-Heap Leak
How to Merge Documents Correctly
Spring 3 MVC: One-To-Many Within a Dynamic Form (Add/Remove on Create/Update)