[英]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 完成对数据的处理后,将其保存到文件中的最佳方法是什么,因此当我再次使用该程序时,可以使用剩下的数据并删除Arrays.fill
(Thank-you) (谢谢)
Use a DataOutputStream and write doubles to it. 使用DataOutputStream并向其写入双打。 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. 不要忘记处理可能发生的IOException,并在完成处理后关闭流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.