Advertisement
ivanov_ivan

SaveAnArrayListOfDoubles

Mar 13th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class HW05SaveAnArrayListOfDoubles {
  8.     public static void main(String[] args) {
  9.        
  10.         File FILE_PATH = new File("resources/doubles.list");
  11.         File SAVE_PATH = new File("resources/doubles.list");
  12.  
  13.        
  14.         List<Double> arrList = new ArrayList<>();
  15.         arrList.add(2.5D);
  16.         arrList.add(7.2D);
  17.         arrList.add(7.659D);
  18.         arrList.add(100.598D);
  19.  
  20.         try {
  21.             ObjectOutputStream writer = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(SAVE_PATH)));
  22.             for (Object ob: arrList) {
  23.                 writer.writeObject(ob);
  24.             }
  25.             arrList.clear();
  26.             writer.flush();
  27.         } catch (IOException e) {
  28.             System.out.println("IOException from writer");
  29.         }
  30.  
  31.         try {
  32.             ObjectInputStream reader = new ObjectInputStream(new BufferedInputStream(new FileInputStream(FILE_PATH)));
  33.             Object obj;
  34.             while((obj = reader.readObject()) != null){
  35.                 String str = obj.toString();
  36.                 arrList.add(Double.parseDouble(str));
  37.             }
  38.         } catch (EOFException e){
  39.  
  40.             /*System.out.println("EOFException from reader");
  41.             Catch Exception without print StackTrace ... The program continue without mistakes?!?
  42.              */
  43.  
  44.         } catch (IOException e) {
  45.  
  46.             System.out.println("IOException from reader");
  47.  
  48.         } catch (ClassNotFoundException e) {
  49.             e.printStackTrace();
  50.         }
  51.  
  52.         for (Double num:arrList) {
  53.             System.out.println(num);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement