java用io流定义一个图书类,有id,name,price,author,type,press,,在测试类里实现添加到book.txt文件中,实现查询功能,删除功能,修改功能
时间: 2024-05-04 22:21:53 浏览: 93
首先,我们定义一个Book类,包含id,name,price,author,type,press属性和相关的getter和setter方法。
```java
public class Book {
private String id;
private String name;
private double price;
private String author;
private String type;
private String press;
public Book() {}
public Book(String id, String name, double price, String author, String type, String press) {
this.id = id;
this.name = name;
this.price = price;
this.author = author;
this.type = type;
this.press = press;
}
// getter and setter methods
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
}
```
接下来,我们实现一个BookManager类,负责对book.txt文件进行读取、添加、查询、删除和修改操作。具体代码如下:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class BookManager {
private static final String FILE_NAME = "book.txt";
private static final String FIELD_SEPARATOR = ",";
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
// 读取所有的书籍信息
public static List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(FIELD_SEPARATOR);
if (fields.length == 6) {
Book book = new Book(fields[0], fields[1], Double.parseDouble(fields[2]), fields[3], fields[4], fields[5]);
bookList.add(book);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return bookList;
}
// 添加一本书籍
public static void addBook(Book book) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true))) {
writer.write(book.getId() + FIELD_SEPARATOR + book.getName() + FIELD_SEPARATOR
+ book.getPrice() + FIELD_SEPARATOR + book.getAuthor() + FIELD_SEPARATOR
+ book.getType() + FIELD_SEPARATOR + book.getPress() + LINE_SEPARATOR);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// 根据id查询一本书籍
public static Book getBookById(String id) {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(FIELD_SEPARATOR);
if (fields.length == 6 && fields[0].equals(id)) {
return new Book(fields[0], fields[1], Double.parseDouble(fields[2]), fields[3], fields[4], fields[5]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 根据id删除一本书籍
public static void deleteBookById(String id) {
List<Book> bookList = getAllBooks();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Book book : bookList) {
if (!book.getId().equals(id)) {
writer.write(book.getId() + FIELD_SEPARATOR + book.getName() + FIELD_SEPARATOR
+ book.getPrice() + FIELD_SEPARATOR + book.getAuthor() + FIELD_SEPARATOR
+ book.getType() + FIELD_SEPARATOR + book.getPress() + LINE_SEPARATOR);
}
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// 根据id修改一本书籍的信息
public static void updateBookById(String id, Book newBook) {
List<Book> bookList = getAllBooks();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Book book : bookList) {
if (book.getId().equals(id)) {
writer.write(newBook.getId() + FIELD_SEPARATOR + newBook.getName() + FIELD_SEPARATOR
+ newBook.getPrice() + FIELD_SEPARATOR + newBook.getAuthor() + FIELD_SEPARATOR
+ newBook.getType() + FIELD_SEPARATOR + newBook.getPress() + LINE_SEPARATOR);
} else {
writer.write(book.getId() + FIELD_SEPARATOR + book.getName() + FIELD_SEPARATOR
+ book.getPrice() + FIELD_SEPARATOR + book.getAuthor() + FIELD_SEPARATOR
+ book.getType() + FIELD_SEPARATOR + book.getPress() + LINE_SEPARATOR);
}
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
最后,我们可以在测试类中使用BookManager类的方法进行相关的操作。
```java
public class Test {
public static void main(String[] args) {
// 添加一本书籍
Book book1 = new Book("001", "Java编程思想", 99.0, "Bruce Eckel", "计算机技术", "机械工业出版社");
BookManager.addBook(book1);
// 查询一本书籍
Book book2 = BookManager.getBookById("001");
System.out.println(book2.getName());
// 修改一本书籍的信息
Book newBook = new Book("001", "Java编程思想(第4版)", 109.0, "Bruce Eckel", "计算机技术", "机械工业出版社");
BookManager.updateBookById("001", newBook);
// 删除一本书籍
BookManager.deleteBookById("001");
}
}
```
阅读全文