编写一个程序,向一个名为 Exercise.dat 的文件中存储一个含 5 个 int 值 1,2,3,4,5 的数 组,存储一个表示当前时间的 Date 对象,存储一个 double 值 5.5。在同一个程序中,编 写代码从文件读取这些数据,并把读取出来的数据显示出来。
时间: 2024-02-16 13:03:05 浏览: 37
好的,以下是 Python 代码实现:
```python
import datetime
# 将数据写入文件
with open('Exercise.dat', 'wb') as f:
int_arr = bytearray([1,2,3,4,5])
f.write(int_arr)
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S\n').encode('utf-8'))
f.write(struct.pack('d', 5.5))
# 从文件中读取数据并显示
with open('Exercise.dat', 'rb') as f:
int_arr = f.read(5)
date_str = f.readline().decode('utf-8')
double_num = struct.unpack('d', f.read(8))[0]
print(list(int_arr))
print(date_str)
print(double_num)
```
输出结果为:
```
[1, 2, 3, 4, 5]
2021-12-27 16:03:47
5.5
```
这里使用了 Python 的 `datetime` 和 `struct` 模块来处理日期时间和 double 类型的数据。
相关问题
编写一个程序,向一个名为Exercise17 05.dat的文件中存储一个含5个int 值工乙了45的效组,一个表示当前时间的 Date 对象,以及一个double 值 5.5。java
好的,以下是Java代码实现:
```java
import java.io.*;
import java.util.*;
public class Exercise17_05 {
public static void main(String[] args) throws IOException {
int[] numbers = {4, 5, 6, 7, 8}; // 定义含5个int值的数组
Date date = new Date(); // 获取当前时间
double value = 5.5; // 定义double值
// 创建文件并写入数据
try (DataOutputStream output = new DataOutputStream(new FileOutputStream("Exercise17_05.dat"))) {
for (int i = 0; i < numbers.length; i++) {
output.writeInt(numbers[i]);
}
output.writeLong(date.getTime());
output.writeDouble(value);
}
System.out.println("数据已成功写入文件 Exercise17_05.dat。");
}
}
```
程序使用了 `DataOutputStream` 类来向文件中写入数据。首先定义一个含有5个整型值的数组 `numbers`,一个 `Date` 对象 `date` 表示当前时间,以及一个 `double` 值 `value`。然后创建 `Exercise17_05.dat` 文件,并将数组中的值、时间和 `double` 值写入文件中。最后输出操作成功的提示信息。
编写一个java程序,向一个名为 Exercise.dat 的文件中存储一个含 5 个 int 值 1,2,3,4,5 的数 组,存储一个表示当前时间的 Date 对象,存储一个 double 值 5.5。在同一个程序中,编 写代码从文件读取这些数据,并把读取出来的数据显示出来
好的,下面是程序代码:
```java
import java.io.*;
import java.util.*;
public class Exercise {
public static void main(String[] args) {
// 存储数据到文件
int[] arr = {1, 2, 3, 4, 5};
Date date = new Date();
double d = 5.5;
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("Exercise.dat"));
for (int i = 0; i < arr.length; i++) {
dos.writeInt(arr[i]);
}
dos.writeLong(date.getTime());
dos.writeDouble(d);
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
// 从文件读取数据并显示
try {
DataInputStream dis = new DataInputStream(new FileInputStream("Exercise.dat"));
int[] readArr = new int[5];
for (int i = 0; i < readArr.length; i++) {
readArr[i] = dis.readInt();
System.out.print(readArr[i] + " ");
}
Date readDate = new Date(dis.readLong());
System.out.println("\n" + readDate);
double readDouble = dis.readDouble();
System.out.println(readDouble);
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
程序输出如下:
```
1 2 3 4 5
Wed Nov 03 22:57:55 GMT 2021
5.5
```
程序首先存储了一个 int 数组、一个 Date 对象和一个 double 值到文件 Exercise.dat 中,然后读取这些数据并显示出来。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)