Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- public class HW05SaveAnArrayListOfDoubles {
- public static void main(String[] args) {
- File FILE_PATH = new File("resources/doubles.list");
- File SAVE_PATH = new File("resources/doubles.list");
- List<Double> arrList = new ArrayList<>();
- arrList.add(2.5D);
- arrList.add(7.2D);
- arrList.add(7.659D);
- arrList.add(100.598D);
- try {
- ObjectOutputStream writer = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(SAVE_PATH)));
- for (Object ob: arrList) {
- writer.writeObject(ob);
- }
- arrList.clear();
- writer.flush();
- } catch (IOException e) {
- System.out.println("IOException from writer");
- }
- try {
- ObjectInputStream reader = new ObjectInputStream(new BufferedInputStream(new FileInputStream(FILE_PATH)));
- Object obj;
- while((obj = reader.readObject()) != null){
- String str = obj.toString();
- arrList.add(Double.parseDouble(str));
- }
- } catch (EOFException e){
- /*System.out.println("EOFException from reader");
- Catch Exception without print StackTrace ... The program continue without mistakes?!?
- */
- } catch (IOException e) {
- System.out.println("IOException from reader");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- for (Double num:arrList) {
- System.out.println(num);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement