面向流的编程和对象序列化--------- 我们的java实验,求好心人帮我做一下。

实验目的:1、掌握Java流的工作方式;2、掌握常用输入和输出流的使用;3、掌握文件的访问、目录和文件管理类的使用实验内容:1、编写程序实现以下功能:(1)提示用户输入文... 实验目的:

1、掌握Java流的工作方式;

2、掌握常用输入和输出流的使用;

3、掌握文件的访问、目录和文件管理类的使用

实验内容:

1、编写程序实现以下功能:
(1)提示用户输入文件要存储的路径(例如c:\test)
(2)检查用户指定的路径中的文件夹是否存在,如果不存在,则创建这个文件夹
(3)提示用户输入文件名称
(4)检查步骤(1)的文件夹是否存在此文件名的文件,如果存在,则提示用户文件已经存在,要求用户重新输入,返回步骤(3)
(5)提示用户分别录入图书名称,出版社,价格,数量,是否教材(boolean类型)等信息
(6)将用户录入的图书信息用文件流的方式写入到步骤(4)确定的文件中
(7)询问用户是否结束录入图书信息,如果用户输入n,则返回步骤(5),否则关闭文件
(8)从用户输入的文件中把用户输入的图书信息读取,逐行显示在屏幕上,并最后显示图书的个数

2、过程同上题,但是要求图书信息的写入和读取采用对象的方式,即ObjectOutputStream、ObjectInputStream(提示:需要定义图书信息类并实现序列化接口)
展开
 我来答
Irreappearable
2012-03-17 · TA获得超过4956个赞
知道大有可为答主
回答量:1423
采纳率:25%
帮助的人:3107万
展开全部
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test
{
static class Book implements Serializable {
String name, publisher;
double price;
int copy;
boolean isTextBook;
}

public static void main(String[] args) throws Exception
{
Scanner scanner = new Scanner(System.in);
File file = getFile(scanner, getDirectory(scanner));
List<Book> books = getBooks(scanner);

//1.
//saveWithFileStream(file, books);
//readWithFileStream(file);

//2.
saveWithSerializer(file, books);
readWithSerializer(file);
}

private static File getDirectory(Scanner scanner) {
System.out.println("Directory: ");
String input = scanner.next();
File dir = new File(input);
if (dir.exists() == false) {
dir.mkdirs();
}
return dir;
}

private static File getFile(Scanner scanner, File dir) throws IOException {
while (true) {
System.out.println("File: ");
String input = scanner.next();
if (dir.list(getFilenameFilter(input)).length == 0) {
File file = new File(dir, input);
file.createNewFile();
return file;
}

System.out.println("File already exists");
}
}

private static FilenameFilter getFilenameFilter(final String filename) {
return new FilenameFilter() {
public boolean accept(File dir, String name) {
return filename.equals(name);
}
};
}

private static List<Book> getBooks(Scanner scanner) {
List<Book> books = new ArrayList<Book>();
while (true) {
Book book = new Book();
System.out.println("Name:");
book.name = scanner.next();
System.out.println("Publisher:");
book.publisher = scanner.next();
System.out.println("Price:");
book.price = scanner.nextDouble();
System.out.println("Copy:");
book.copy = scanner.nextInt();
System.out.println("Is TextBook:");
book.isTextBook = scanner.next().equals("y");
books.add(book);

System.out.println("Finish?");
if (scanner.next().equals("n") == false) {
return books;
}
}
}

private static void saveWithFileStream(File file, List<Book> books)
throws IOException {
FileWriter writer = new FileWriter(file);
for (Book book : books) {
writer.write(book.name + " ");
writer.write(book.publisher + " ");
writer.write(String.valueOf(book.price + " "));
writer.write(String.valueOf(book.copy + " "));
writer.write(String.valueOf(book.isTextBook + " "));
}
writer.close();
}

private static void readWithFileStream(File file) throws FileNotFoundException {
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNext()) {
System.out.println("Name: " + scanner.next());
System.out.println("Publisher: " + scanner.next());
System.out.println("Price: " + scanner.next());
System.out.println("Copy: " + scanner.next());
System.out.println("Is TextBook: " + scanner.next());
System.out.println();
++count;
}

System.out.println("Total: " + count);
}

private static void saveWithSerializer(File file, List<Book> books)
throws IOException {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream writer = new ObjectOutputStream(fos);
for (Book book : books) {
writer.writeObject(book);
}
writer.close();
}

private static void readWithSerializer(File file)
throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);

Object o;
int count = 0;
try {
while ((o = ois.readObject()) != null) {
Book book = (Book) o;
System.out.println("Name: " + book.name);
System.out.println("Publisher: " + book.publisher);
System.out.println("Price: " + book.price);
System.out.println("Copy: " + book.copy);
System.out.println("Is TextBook: " + book.isTextBook);
System.out.println();
++count;
}
} catch (EOFException ee) {}

System.out.println("Total: " + count);
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式