java2019. 9. 22. 15:01

Serializable interface, transient 예약어

FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream 예제

ex)



public class SerialDTO implements Serializable { //Serializable를 반드시 구현해야 ObjectStream class들을 이용가능하다.
	
	private String bookName;
	private int bookOrder;
	//transient private int bookOrder;// transient로 선언된 변수는 저장 되지 않는다 (security) 
	
	
	private String bookType="IT";//추가시  local class incompatible 발생
	static final long serialVersionUID = 1l; //serialVersionUID를 명시적으로 설정하면 class에 변경이 생겨도 같은 객체로 인식하여 로딩이 가능해진다.
	
	public SerialDTO( String bookName , int bookOrder) {
		super();
		this.bookName = bookName;
		this.bookOrder= bookOrder;
	}
	
	@Override
	public String toString() {

		
		//return String.format("bookName : %s bookOrder : %d",bookName,bookOrder);
		return String.format("bookName : %s bookOrder : %d bookType : %d",bookName,bookOrder,bookType);
	}

}


public class ManagerObject {

	
	
	public void runManager() {
		String fullPath = "C:"+File.separator+"Users"+File.separator+"Lee"+File.separator+"test"+File.separator+"Test.obj";
		SerialDTO dto = new SerialDTO("fantasy", 123);
		
		//saveObject(fullPath ,dto);
		loadObject(fullPath);
	}
	private void saveObject( String fullPath, SerialDTO dto) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		
		try {
			fos = new FileOutputStream(fullPath);//FileOutputStream(fullPath) 객체를 생성후, ObjectOutputStream 생성자에 전달 
			oos = new ObjectOutputStream(fos);
			oos.writeObject(dto);//쓰기
			System.out.println("write done");
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if ( fos != null)
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if( oos!= null)
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		
	}

	private void loadObject(String fullPath) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(fullPath);
			ois = new ObjectInputStream(fis);
			
			
			try {
				SerialDTO dto = (SerialDTO)ois.readObject();
				System.out.println(dto);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if( fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if ( ois != null)
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		
		
		
	}

	
	
		
		
	
}

출처 : 자바의 신

'java' 카테고리의 다른 글

Buffer class의 이해  (0) 2019.09.22
NioSample  (0) 2019.09.22
FileReader 및 BufferedReader + (Scanner)  (0) 2019.09.22
FileWriter 및 BufferedWriter 텍스트기반 처리  (0) 2019.09.22
FileFilter , FileNameFilter interface  (0) 2019.09.21
Posted by easy16