Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.5 kB
0
Indexable
Never
package homework;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;

public class main {

    public static void main(String[] args) {
         Formatter formatter = null;
        try {
             formatter = new Formatter("Library.txt");

            ArrayList<Library> list = new ArrayList();
            list.add(new Library("Fahad", 1, 400));
            list.add(new Library("PNU", 2, 300));

            for (int i = 0; i < list.size(); i++) {
                formatter.format("%s", list.get(i));
            }

        } catch (FileNotFoundException ex) {
            System.err.println("Error: " + ex.getMessage());
        } finally {
            if (formatter != null) {
                System.out.println("All data has been saved successfully!");
                formatter.close();
            }
        }

        Scanner input;
        try {

             
            input = new Scanner(new File("Library.txt"));
            
            while (input.hasNext()) {
                String name = input.next();
                int id = input.nextInt();
                int dep = input.nextInt();
                Library lib = new Library(name, id, dep);
                System.out.println(lib);

            }
        } catch (FileNotFoundException ex) {
            System.err.println("Error: " + ex.getMessage());
        }

    }

}