Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HashMap & File i/o: Please critique... 2

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
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.
 
use the Input/ObjectOutputStream streams to wrtie and read java object to/from file. see java.io_ObjectOutputStream

e.g.

FileOutputStream ostream = new FileOutputStream("t.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);

p.writeInt(12345);
p.writeObject("Today");
p.writeObject(new Date());

p.flush();
ostream.close();

to see if a file exists, u can use the File class, also in the io package. if the file doesn't exist, use the createNewFile method.

all this is docmented very well in the system javadoc
 
Thanks. I recognize your example from Sun's javadoc. What exactly is .tmp?

I need to specify a file type when creating the new file, right? So, if my object is a hashmap, what kind of file am I creating? Is it a .dat file, or what?

Please excuse my ignorance.
 
Hi,
The file extension can be whatever you want. It only used when some other program is binded to that extension, for example .txt files are often opened with notepad in Windows etc.
So the file extension doesn't actually have influence to the content of the file -> content remains the same but the program used automatically to open the file may change.
I suppose you use only your own java-applications to open these files so you can use any extension you want.

-Vepo
 
as vepo says, you can use whatever extension you like. even myFile.hashmap will do. its up to you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top