Advertisement
Guest User

Deleting from a ".txt" file

a guest
Nov 30th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package javahelp;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.Serializable;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class Javahelp {
  13.  
  14.    
  15.     public static void main(String[] args) {
  16.        
  17.         Customer c1 = new Customer(0, "Smith", "smith@smith.com", "123456789", "details about smith");
  18.         Customer c2 = new Customer(1, "Jons", "jones@jones.com", "234567891", "details about jones");
  19.         Customer c3 = new Customer(2, "Williams", "williams@williams.com", "345678912", "details about williams");
  20.        
  21.         List<Customer> customerList = new ArrayList<>();
  22.         customerList.add(c1);
  23.         customerList.add(c2);
  24.         customerList.add(c3);
  25.        
  26.         Customers customers = new Customers(customerList);
  27.        
  28.         System.out.println("Saving customers 0, 1 and 2 to file...");
  29.        
  30.         customers.saveCustomers();
  31.        
  32.         /* ----- */
  33.        
  34.         System.out.println("Reading customers 0, 1 and 2 from file...");
  35.        
  36.         customers.readCustomers();
  37.         customers.printCustomers();
  38.        
  39.         Customer c4 = new Customer(3, "Taylor", "taylor@taylor.com", "456789123", "details about taylor");
  40.         customers.addCustomer(c4);
  41.        
  42.         customers.saveCustomers();
  43.        
  44.         System.out.println("Saving customer 3 to file...");
  45.        
  46.         /* ----- */
  47.        
  48.         System.out.println("Reading customers 0, 1, 2 and 3 from file...");
  49.        
  50.         customers.readCustomers();
  51.         customers.printCustomers();
  52.        
  53.         /* ----- */
  54.        
  55.         System.out.println("Removing customer with id = 1 from file...");
  56.        
  57.         customers.removeCustomerById(1);
  58.         customers.saveCustomers();
  59.        
  60.         /* ----- */
  61.        
  62.        
  63.         System.out.println("Reading customers 0, 2 and 3 from file...");
  64.        
  65.         customers.readCustomers();
  66.         customers.printCustomers();
  67.        
  68.        
  69.     }
  70.    
  71.    
  72. }
  73.  
  74. class Customers {
  75.    
  76.     private final File file = new File("D:\\customers.txt");
  77.     private List<Customer> customers;
  78.    
  79.     public Customers(List<Customer> customers) {
  80.        
  81.         this.customers = customers;
  82.    
  83.     }
  84.    
  85.     public void saveCustomers() {
  86.        
  87.         try {
  88.            
  89.             if (!file.exists()) {
  90.                 file.createNewFile();
  91.             }
  92.            
  93.             FileOutputStream fos = new FileOutputStream(file);
  94.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  95.            
  96.             oos.writeObject(this.customers);
  97.             oos.close();
  98.            
  99.         } catch (Exception e) {
  100.            
  101.             e.printStackTrace();
  102.             System.out.println("Could not write object");
  103.        
  104.         }
  105.        
  106.     }
  107.    
  108.     public List<Customer> readCustomers() {
  109.        
  110.         try {
  111.            
  112.             FileInputStream fis = new FileInputStream(file);
  113.             ObjectInputStream ois = new ObjectInputStream(fis);
  114.            
  115.             this.customers = (List<Customer>)ois.readObject();
  116.             ois.close();
  117.            
  118.         } catch (Exception e) {
  119.            
  120.             e.printStackTrace();
  121.             System.out.println("Could not read file");
  122.            
  123.         }
  124.        
  125.         return this.customers;
  126.        
  127.     }
  128.    
  129.     public void addCustomer(Customer customer) {
  130.        
  131.         this.customers = readCustomers();
  132.         this.customers.add(customer);
  133.        
  134.        
  135.     }
  136.    
  137.    
  138.     public void removeCustomerById(int id) {
  139.        
  140.         this.customers = readCustomers();
  141.        
  142.         List<Customer> list = new ArrayList<>();
  143.         for(Customer customer : this.customers) {
  144.            
  145.             if(customer.getId() != id) {
  146.                
  147.                 list.add(customer);
  148.            
  149.             }
  150.            
  151.         }
  152.        
  153.         this.customers = list;
  154.        
  155.     }
  156.    
  157.     public void printCustomers() {
  158.        
  159.         for (Customer customer : this.customers) {
  160.            
  161.             customer.printCustomer();
  162.            
  163.         }
  164.        
  165.     }
  166.    
  167. }
  168.  
  169. class Customer implements Serializable {
  170.  
  171.     private static final long serialVersionUID = 7797875735963352544L;
  172.     private int id;
  173.     private String name;
  174.     private String email;
  175.     private String phone;
  176.     private String details;
  177.    
  178.     public Customer(int id, String name, String email, String phone, String details) {
  179.         super();
  180.         this.id = id;
  181.         this.name = name;
  182.         this.email = email;
  183.         this.phone = phone;
  184.         this.details = details;
  185.     }
  186.    
  187.     public int getId() {
  188.         return this.id;
  189.     }
  190.    
  191.     public void printCustomer() {
  192.        
  193.         System.out.println("");
  194.         System.out.println("////////////////////");
  195.         System.out.println("id: " + this.id);
  196.         System.out.println("name: " + this.name);
  197.         System.out.println("email: " + this.email);
  198.         System.out.println("phone: " + this.phone);
  199.         System.out.println("details: " + this.details);
  200.         System.out.println("////////////////////");
  201.         System.out.println("");
  202.        
  203.     }
  204.    
  205.    
  206.    
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement