파일 작성
<%
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
System.out.println(appRoot);
File newFile = new File(appRoot, "objData/data.ser");
if( !newFile.getParentFile().exists())
{
newFile.getParentFile().mkdirs();
}
Hashtable<String, String> h = new Hashtable<String, String>();
h.put("key1", "사과");
h.put("key2", "수박");
FileOutputStream fos = new FileOutputStream(newFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(h);
oos.close();
fos.close();
%>
파일 읽기
<%
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
File newFile = new File(appRoot, "objData/data.ser");
if (newFile.exists())
{
FileInputStream fis = new FileInputStream(newFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Hashtable h = (Hashtable)ois.readObject();
ois.close();
fis.close();
Enumeration e = h.keys();
while(e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = (String)h.get(key);
%>
<%=key %>의 값은 <%=value %> 입니다. <br>
<%
}
}
else
{
out.println("해당 파일이 존재하지 않습니다.");
}
%>