this is your problem out = new FileOutputStream("inventory.dat"); .
Try this and see what happends out = new FileOutputStream("C:" + File.separator + "inventory.dat");
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
No, Java can't create directories under DOS for the very simple reason that Java doesn't work under DOS.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
OK, with the fileoutput stream I was able to write the file, but not create the directory.
Use a separate File object for the directory. Java treats files and directories both as File. With the new File statement I am able to create the directory but not write the file.
Your code wrote to the console - just like you told it to.
System.out.println(listModel);
I assume you are working from a class example, but you are replacing crucial pieces with chunks of example code here with no thought to what they are actually doing.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Ez-
That is not entirely true. I do not replace any code without thinking it through. The problem for me is that, besides here, I have absolutley NO instruction on how to do any of this. A month ago I did not even know what a method was...
My apologies for the particular choice of words.
Glad you got it to work. A couple of suggestions:
// SAVE
private void btnSaveActionPerformed(ActionEvent evt)
{
// use variable names that have meaning in your context
File dataDir = new File("C:" + File.separator + "data");
try
{
if( !dataDir.exists() )// shorter expresion with ! operator
{
dataDir.mkdir();// make directory
// toWork.createNewFile(); // create file (don't need)
}
if( !dataDir.canRead() )
System.out.println("Read Error.");
if( !toWork.canWrite())
System.out.println("Write error.");
}
catch (Exception e){
// generally not good to catch exceptions and do nothing
}
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try
{
// create a new file output stream
// connected to "inventory.dat"
// can use dataDir here and just append file name
out = new FileOutputStream(dataDir + File.separator + "inventory.dat");
// connect print stream to the output stream
p = new PrintStream(out);
p.println (listModel);
p.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
}// end SAVE
Have you looked at the output in your data file yet? It might not be what you expect because the entire listModel.toString() is being dumped to the println() method. You may want to write each cd individually
for (int i=0; i<listModel.size(); i++) {
p.println( listModel.getElementAt(i).toString() );
}
which would put your toString() description of each cd onto it's own line. It the data needed to be retrieved and loaded later, you would probably want to write each item as comma or tab separated values (i.e. name, artist, price, etc.), but that is a different consideration.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847