创建Book类,成员属性有:书号、书名、作者、价格、出版社、出版年份。 创建该类的10个对象。将这10个对象的成员属性值按类型存入文件中并读出,用java实现
时间: 2024-06-10 17:05:14 浏览: 206
好的,我明白了,接下来我将用 ChitGPT AI 与您进行对话,回答您的问题。
首先,我们需要定义一个 Book 类,包含书号、书名、作者、价格、出版社、出版年份等成员属性。
代码如下:
```java
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private int bookId;
private String bookName;
private String author;
private double price;
private String publisher;
private int publishYear;
public Book(int bookId, String bookName, String author, double price, String publisher, int publishYear) {
this.bookId = bookId;
this.bookName = bookName;
this.author = author;
this.price = price;
this.publisher = publisher;
this.publishYear = publishYear;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", author=" + author + ", price=" + price
+ ", publisher=" + publisher + ", publishYear=" + publishYear + "]";
}
}
```
在这个类中,我们使用了 Serializable 接口,在将对象写入文件和读取文件时会用到它。
接下来,我们可以使用以下代码创建 10 个 Book 对象并将它们写入文件。
```java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class WriteToFile {
public static void main(String[] args) throws Exception {
Book[] books = new Book[10];
books[0] = new Book(1, "Java核心技术 卷I", "Cay S. Horstmann,Gary Cornell", 79.9, "机械工业出版社", 2019);
books[1] = new Book(2, "Java编程思想", "Bruce Eckel", 108.0, "机械工业出版社", 2007);
books[2] = new Book(3, "Java 8编程实战", "Raoul-Gabriel Urma,Mario Fusco,Alan Mycroft", 89.9, "人民邮电出版社", 2018);
books[3] = new Book(4, "深度学习", "Ian Goodfellow,Yoshua Bengio,Aaron Courville", 119.0, "人民邮电出版社", 2017);
books[4] = new Book(5, "计算机网络 自顶向下方法", "James F. Kurose,Keith W. Ross", 119.0, "机械工业出版社", 2018);
books[5] = new Book(6, "Linux高级程序设计", "Mark L. Mitchell,Alex Samuel", 89.9, "人民邮电出版社", 2014);
books[6] = new Book(7, "编译原理", "Alfred V. Aho, Monica S. Lam", 119.0, "机械工业出版社", 2018);
books[7] = new Book(8, "软件工程: 一种实践方法", "Roger S. Pressman", 98.0, "机械工业出版社", 2010);
books[8] = new Book(9, "算法 第四版", "Robert Sedgewick, Kevin Wayne", 138.0, "人民邮电出版社", 2019);
books[9] = new Book(10, "操作系统概念 第10版", "Abraham Silberschatz, Greg Gagne, Peter B. Galvin", 139.0, "机械工业出版社", 2018);
FileOutputStream fos = new FileOutputStream("book.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
for (Book book : books) {
os.writeObject(book);
}
os.close();
fos.close();
}
}
```
在这个示例中,我们先创建了一个 Book 对象数组,然后通过 ObjectOutputStream 写入文件。
使用下面的代码从文件中读取刚刚写入文件的 Book 对象:
```java
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class ReadFromFile {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("book.txt");
ObjectInputStream is = new ObjectInputStream(fis);
for (int i = 0; i < 10; i++) {
Book book = (Book) is.readObject();
System.out.println(book);
}
is.close();
fis.close();
}
}
```
在此示例中,我们使用 FileInputStream 和 ObjectInputStream 来读取文件,并且使用 readObject() 方法读取对象。
最后,我们需要说明这里使用的是基本文件读写操作,实际开发中,还需要考虑各种异常处理、并发访问等问题。
以上是答案,我希望它对您有帮助!
阅读全文