•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,142 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,656 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1900 | Replies: 13 | Solved
![]() |
After my poor showing yesterday I was ready to quit, but I decided this morning I would rather feel dumb than feel like a quitter, so here I am.
I coded this last night thinking if it worked great, if not then I would just drop it. Well it worked ... kind of.
I want to write my JList to a file on the harddrive, so I created a button that does this, and the file is created just fine, in the same directory as all my classes and java files. Not where I want it. I tried changing the path to a c:\data\ directory, but my method will not put the file there, even if I manually create the directory it will not do it.
Why can I not change location of my output? And if I can, can I make JAVA create the directory for me if it is not already there?
I coded this last night thinking if it worked great, if not then I would just drop it. Well it worked ... kind of.
I want to write my JList to a file on the harddrive, so I created a button that does this, and the file is created just fine, in the same directory as all my classes and java files. Not where I want it. I tried changing the path to a c:\data\ directory, but my method will not put the file there, even if I manually create the directory it will not do it.
private void btnSaveActionPerformed(ActionEvent evt)
{
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try
{
// create a new file output stream
// connected to "myfile.txt"
out = new FileOutputStream("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 SAVEWhy can I not change location of my output? And if I can, can I make JAVA create the directory for me if it is not already there?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation:
Rep Power: 11
Solved Threads: 296
this is your problem
Try this and see what happends
out = new FileOutputStream("inventory.dat");. Try this and see what happends
out = new FileOutputStream("C:" + File.separator + "inventory.dat"); Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Just for future reference, you can't give Java a relative reference to a file and expect it to create a file with an absolute reference.
"inventory.dat" will be created in the folder that you are running java from (relative reference, relative to the creating location)
"C:\inventory.dat" will be created in the C: (absolute reference, doesn't matter where the creator is)
"inventory.dat" will be created in the folder that you are running java from (relative reference, relative to the creating location)
"C:\inventory.dat" will be created in the C: (absolute reference, doesn't matter where the creator is)
Thanks for the tip on relative and absolute references.
I added to your line with this:
In order to get it down one more level into my DATA folder. It works, as long as the data folder is present upon execution.
Can I not make Java create that folder if it is not already present?
I added to your line with this:
out = new FileOutputStream("C:" + File.separator + "data" + File.separator + "inventory.dat");In order to get it down one more level into my DATA folder. It works, as long as the data folder is present upon execution.
Can I not make Java create that folder if it is not already present?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
public void createFile(String in)
{
toWork = new File(in);
try
{
if(toWork.exists() == false)
{
System.out.println("DNE");
toWork.createNewFile();
}
if(toWork.canRead() == false)
System.out.println("Read error.");
if(toWork.canWrite() == false)
System.out.println("Write error.");
}
catch(Exception e){}
}That was an handling function I had for an old network isntant messaging program I wrote. Try doing the same thing, creating the FileOutputStream object from a File object after checking if the directory/file exists. If it doesn't exist, use the "mkdir()" method from File to create the directory.
Last edited by TheGathering : Aug 3rd, 2007 at 11:02 am.
Something new. It did not error durning compile. good sign. It did not error during test run, another good sign.
It did not create file or directory either though!
I have done a lot of wrong stuff out here, but I cannot wait to see how I managed this.
p.s. I tried the file name both ways, just incase I misunderstood what you told me earlier.
It did not create file or directory either though!
I have done a lot of wrong stuff out here, but I cannot wait to see how I managed this.
// SAVE
private void btnSaveActionPerformed(ActionEvent evt)
{
File toWork = new File("C:" + File.separator + "data" + File.separator + "inventory.dat");
try
{
if(toWork.exists() == false)// if file and directory do not exist
{
toWork.mkdir();// make directory
toWork.createNewFile(); // create file
System.out.println(listModel); // print list to file
}
if(toWork.canRead()== false)
System.out.println("Read Error.");
if(toWork.canWrite() == false)
System.out.println("Write error.");
}
catch (Exception e){}
}// end SAVEp.s. I tried the file name both ways, just incase I misunderstood what you told me earlier.
Last edited by no1zson : Aug 3rd, 2007 at 12:02 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
I am most confused then, because I am supposed to use exception handling to create the file and directory if they do not exist.
Maybe I will just put up a message if it is not there telling the user to create it manually and then try again.
Maybe I will just put up a message if it is not there telling the user to create it manually and then try again.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
Actually, as I continued to play with it I DID get it to create my data directory by altering this line.
File toWork = new File("C:" + File.separator + "data");
Only problem is that my string now prints to the screen in stead of a file.
File toWork = new File("C:" + File.separator + "data");
Only problem is that my string now prints to the screen in stead of a file.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: Unicode, CSV
- Next Thread: Convert User Supplied Int to App Generated



Linear Mode