MissouriTiger
Programmer
I'm not sure if I'm doing this correctly. Could someone please point out any mistakes I'm making.
I'm trying to write a HashMap object to the hard drive. And another method gets the HashMap from the hard drive.
I'm confused about naming the file. Do I need to include the file type (such as .txt or .exe)? If so, what type is it? What kind of file type is a java object? Or do I leave that to the OS?
I also don't understand how to write the file if it doesn't already exist. I am trying to use a mkdir method, but the compiler doesn't seem to like it.
Here's my code for writing to hard drive, it is my constructor:
private Map community;
public IMCommunity(Map t){
try
{
File f = new File("communityFile"
if (f.exists()){
FileInputStream iStream = new FileInputStream ("communityFile"
ObjectInputStream oStream = new ObjectInputStream(iStream);
community = (HashMap)oStream.readObject();
iStream.close();
//community.putAll(iStream);}
if (!f.canRead()){
throw (new IOException("File not readable: " + community)); }
if (!f.exists()){
throw (new FileNotFoundException());}
}
catch (IOException e){
System.out.println("IOERROR: " + e.getMessage() + "\n"
}
catch (FileNotFoundException e){
community = new HashMap();
mkdir("communityFile"
}
}
The code to write it to hard drive is:
public void updateFile()
{
FileOutputStream fOutStream = new FileOutputStream("communityFile"
ObjectOutputStream oOutStream = new ObjectOutputStream(fOutStream);
oOutStream.writeObject(community);
oOutStream.flush();
oOutStream.close();
}
I would appreciate any suggestions.
I'm trying to write a HashMap object to the hard drive. And another method gets the HashMap from the hard drive.
I'm confused about naming the file. Do I need to include the file type (such as .txt or .exe)? If so, what type is it? What kind of file type is a java object? Or do I leave that to the OS?
I also don't understand how to write the file if it doesn't already exist. I am trying to use a mkdir method, but the compiler doesn't seem to like it.
Here's my code for writing to hard drive, it is my constructor:
private Map community;
public IMCommunity(Map t){
try
{
File f = new File("communityFile"
if (f.exists()){
FileInputStream iStream = new FileInputStream ("communityFile"
ObjectInputStream oStream = new ObjectInputStream(iStream);
community = (HashMap)oStream.readObject();
iStream.close();
//community.putAll(iStream);}
if (!f.canRead()){
throw (new IOException("File not readable: " + community)); }
if (!f.exists()){
throw (new FileNotFoundException());}
}
catch (IOException e){
System.out.println("IOERROR: " + e.getMessage() + "\n"
}
catch (FileNotFoundException e){
community = new HashMap();
mkdir("communityFile"
}
}
The code to write it to hard drive is:
public void updateFile()
{
FileOutputStream fOutStream = new FileOutputStream("communityFile"
ObjectOutputStream oOutStream = new ObjectOutputStream(fOutStream);
oOutStream.writeObject(community);
oOutStream.flush();
oOutStream.close();
}
I would appreciate any suggestions.