Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created July 12, 2016 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/faad27775253ba33fe531a08761492ba to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/faad27775253ba33fe531a08761492ba to your computer and use it in GitHub Desktop.
LibraryController
package main.java.homeWork.library.model;
import main.java.homeWork.library.model.Library;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mykhailov on 03.07.2016.
*/
public class LibraryController {
//Library library = new Library(); Yanchevskaya A.
public void saveIssueReaderToFile(List<? extends Comparable> list, String file) {
File libraryFile = new File(file);
try {
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(libraryFile)));
oos.writeObject(list);
oos.flush();
oos.close();
} catch (IOException e) {
System.out.println(e);
}
if (libraryFile.getAbsoluteFile().length() > 0)
System.out.println("File successfully written - " + libraryFile.getAbsoluteFile().getName());
else System.out.println("File not found written");
}
@SuppressWarnings("unchecked")
public void showLibraryFromFile(String file) {
//List<Object> librarylist; Yanchevskaya A.
Library copyLibrary; //Yanchevskaya A.
try {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
copyLibrary = (Library) ois.readObject(); //Yanchevskaya A.
//librarylist = (ArrayList<Object>) ois.readObject();
ois.close();
} catch (IOException e) {
System.out.println(e);
return;
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
return;
}
/*for (Object tmp : librarylist) { //Yanchevskaya A.
System.out.println(tmp);
}*/
//Yanchevskaya A.:
System.out.println("Читатели библиотеки:");
copyLibrary.showReadersLibrary();
System.out.println("Книги библиотеки:");
copyLibrary.showIssueLibrary();
}
public void saveLibraryToFile(Library library, String file) throws FileNotFoundException {
File allLibraryFile = new File(file);
try {
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
oos.writeObject(library);
//oos.writeObject(library.getReaders());
oos.flush();
oos.close();
} catch (IOException e) {
System.out.println(e);
}
if (allLibraryFile.getAbsoluteFile().length() > 0)
System.out.println("File successfully written - " + allLibraryFile.getAbsoluteFile().getName());
else System.out.println("File not found written");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment