package cn.glutnn.jy03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
public class Jy200335 {
public static void main(String[] args) {
// //1.接收控制台的用户输入数据
String in = getInputs();
System.out.println(in);
//2.根据用户的输入数据来创建文件夹
Path dir=makeDir(in);
// System.out.println(dir.toAbsolutePath().toString());
//3.在指定文件夹下创建文本文件
Path file=makeFile(dir,in);
// //4.准备存入文本文件的书籍
Book b1=new Book("1001011", "书1", LocalDate.of(2001, 4, 13));
Book b2=new Book("1001012", "书2", LocalDate.of(2003, 5, 1));
Book b3=new Book("1001013", "书3", LocalDate.of(2010, 4, 23));
Book b4=new Book("1001014", "书4", LocalDate.of(2021, 8, 13));
Book b5=new Book("1001015", "书5", LocalDate.of(2011, 7, 22));
Book b6=new Book("1001015", "书6", LocalDate.of(2016, 8, 22));
TreeSet<Book> books=new TreeSet<Book>(new MyComparable());
books.add(b6);
books.add(b5);
books.add(b4);
books.add(b3);
books.add(b2);
books.add(b1);
// for(Book book:books) {
// System.out.println(book);
//
// }
//5.
writeFile(file,books);
//6.
TreeSet<Book> bookset=readFile(file);
System.out.println("读取到的文本内容如下:");
for(Book book:bookset) {
System.out.println(book);
}
}
private static TreeSet<Book> readFile(Path file) {
TreeSet<Book> bookset = null;
try(InputStream in=new FileInputStream(file.toFile());
ObjectInputStream ois=new ObjectInputStream(in);) {
bookset = (TreeSet<Book>) ois.readObject();
} catch (FileNotFoundException e) {
System.out.println("被读取的文本文件不存在!");
} catch (ClassNotFoundException e) {
System.out.println("强制类型转换失败!");
} catch (IOException e) {
System.out.println("指定文本文件读取失败!");
}
return bookset;
}
private static void writeFile(Path file, TreeSet<Book> books) {
//追加数据写入到文本文件
try(OutputStream io=new FileOutputStream(file.toFile());
ObjectOutputStream oos=new ObjectOutputStream(io);) {
oos.writeObject(books);
System.out.println("写文本文件成功!");
} catch (FileNotFoundException e) {
System.out.println("被写入的文本文件不存在!");
} catch (IOException e) {
System.out.println(file.toAbsolutePath().toString()+"该文本文件写入失败!");
e.printStackTrace();
}
}
private static Path makeFile(Path dir, String in) {
Path file=Paths.get(dir.toAbsolutePath().toString(),in.concat(".txt"));
try {
file = Files.createFile(file);
} catch(FileAlreadyExistsException e){
System.out.println("同名文本文件已经存在!");
}catch (IOException e) {
System.out.println("无法创建指定的文本文件!");
file=null;
}
return file;
}
private static Path makeDir(String in) {
Path dir = Paths.get(in);
try {
dir=Files.createDirectory(dir);
} catch (FileAlreadyExistsException e) {
System.out.println("已经存在同名的文件夹!");
}catch(IOException e) {
System.out.println("无法创建指定文件夹!");
dir=null;
}
return dir;
}
private static String getInputs() {
Scanner scanner = new Scanner(System.in);
boolean flag = false;
String inputs = null;
while (!flag) {
System.out.println(">>>");
inputs = scanner.next();
Pattern pattern = null;
//正则表达式:jy200[3|4][0-9]{2}
flag = pattern.matches("^jy200[3|4][0-9]{2}$", inputs);
// System.out.println("flag的返回值为:" + flag);
if (flag == true) {
break;
}else {
System.err.println("你输入的数据格式错误,要重新输入!");
}
}
scanner.close();
return inputs;
}