Rahmadnet

File and I/O

Jul 11th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package belajar.io;
  2.  
  3. import java.io.EOFException;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.PrintWriter;
  12. import java.util.ArrayList;
  13. import java.util.Scanner;
  14.  
  15. public class FileIO
  16. {
  17.  
  18.     private static Scanner scan;
  19.  
  20.     public static void main(String[] args) throws IOException, ClassNotFoundException
  21.     {
  22.         File file = new File("test.txt");
  23.        
  24.         //Write your name and age to the file
  25.        
  26. //      try
  27. //      {
  28. //          PrintWriter output = new PrintWriter(file);
  29. //          output.println("Rahmad Nasution ");
  30. //          output.println(24);        
  31. //
  32. //          output.close();
  33. //      } catch (IOException ex) {
  34. //          System.out.printf("EROR : %s\n", ex);
  35. //      }
  36. //     
  37. //      // Reads from the file
  38. //      try {
  39. //          scan = new Scanner(file);
  40. //          String name = scan.nextLine();
  41. //          int umur = scan.nextInt();
  42. //     
  43. //          System.out.printf("Name : %s\nUmur : %d\n",name, umur);
  44. //      } catch (FileNotFoundException ex) {
  45. //          System.out.printf("ERROR : %s/n",ex);      
  46. //      }
  47.        
  48.         File file1 = new File("students.txt");
  49.         ArrayList<Student> students = new ArrayList<Student>();
  50.         students.add(new Student("Rahmad",3.921));
  51.         students.add(new Student("Rahmadi", 4.0));
  52.         students.add(new Student("Andari", 2.0));
  53.         //serialize the collection of student
  54.         FileOutputStream fo = new FileOutputStream(file1);
  55.         ObjectOutputStream output1 = new ObjectOutputStream(fo);
  56.         for(Student s : students)
  57.         {
  58.             output1.writeObject(s);
  59.         }
  60.        
  61.         output1.close();
  62.         fo.close();
  63.         //deserelize the file back into the collection
  64.         FileInputStream fi = new FileInputStream(file1);
  65.         ObjectInputStream input1 = new ObjectInputStream(fi);
  66.        
  67.         ArrayList<Student> students2 = new ArrayList<Student>();
  68.         try {
  69.             while (true)
  70.             {
  71.                 Student s1 = (Student)input1.readObject();
  72.                 students2.add(s1);
  73.             }
  74.            
  75.         } catch (EOFException ex) {
  76.             // TODO: handle exception
  77.         }
  78.         for(Student s : students2)
  79.         {
  80.             System.out.println(s);
  81.         }
  82.        
  83.     }
  84.  
  85. }
  86.  
  87. import java.io.Serializable;
  88.  
  89. public class Student implements Serializable{
  90.     private String Name;
  91.     private double GPA;
  92.    
  93.     public Student(String name, double gpa) {
  94.         this.Name = name;
  95.         this.GPA = gpa;
  96.     }
  97.  
  98.     public String getName() {
  99.         return this.Name;
  100.     }
  101.  
  102.     public String setName(String newname) {
  103.         return (this.Name = newname);
  104.     }
  105.  
  106.     public double getGPA() {
  107.         return this.GPA;
  108.     }
  109.  
  110.     public double setGPA(double newGPA) {
  111.         return (this.GPA = newGPA);
  112.     }
  113.    
  114.     @Override
  115.     public String toString()
  116.     {
  117.         return String.format("%s\t%f", this.Name, this.GPA);
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment