简体   繁体   中英

Java Read/Write a double array to save when program terminates

It's partially a duplicate but i haven't been able to figure any other ones out. If I have an array

static double saving[]= new double[10000];
Arrays.fill(saving, 5000);

what would be the best way to save it to a file after I finish manipulating the data so when I use the program again I can use the data I left off with and remove the Arrays.fill

(Thank-you)

Use a DataOutputStream and write doubles to it. For example:

DataOutputStream output = new DataOutputStream(new FileOutputStream("your/file/name"));
for(int i = 0; i < saving.length; ++i)
{
    output.writeDouble(saving[i]);
}

Then when loading your program, use the exact opposite:

DataInputStream input = new DataInputStream(new FileInputStream("your/file/name"));
for(int i = 0; i < saving.length; ++i)
{
    saving[i] = input.readDouble();
}

Don't forget to handle the IOExceptions that might occur, and close the streams once you're done with them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM