작업을 하다보면 시간이 오래 걸려서 나오는 결과값들이 있을수 있다. 

 

2시간 걸려 작업한 결과 값이 에러가 나서 중간에 멈추었을경우 

다시 2시간 작업을 시작할것인가??

이를 방지하기위해 작업 중간단계에 저장을 하는방식을 주로 사용하고 있다. (저 같은 경우에는)

 

이때 작업 결과값을 밑에 방식을 통해 저장, 불러올수 있다. 

파일 경로에 대해서는 조만간 다시 다뤄보도록 하겠다.

 

    public Object file2Object(String filePath) {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            ObjectInputStream ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void object2File(Object obj, String filePath) {
        ObjectOutputStream outputStream;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath));
            outputStream.writeObject(obj);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

작업 예시 

 

 

 

 

위 방식은 Java 기본 제공 Object IO 이지만 속도 이슈등이 문제가 있어 아래 라이브러리 사용을 추천함.

https://github.com/RuedigerMoeller/fast-serialization

+ Recent posts