Can JAVA create DOS directories?

Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Can JAVA create DOS directories?

 
0
  #1
Aug 3rd, 2007
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.
  1. private void btnSaveActionPerformed(ActionEvent evt)
  2. {
  3. FileOutputStream out; // declare a file output object
  4. PrintStream p; // declare a print stream object
  5.  
  6. try
  7. {
  8. // create a new file output stream
  9. // connected to "myfile.txt"
  10. out = new FileOutputStream("inventory.dat");
  11.  
  12. // connect print stream to the output stream
  13. p = new PrintStream(out);
  14.  
  15. p.println (listModel);
  16. p.close();
  17. }
  18.  
  19. catch (Exception e)
  20. {
  21. System.err.println ("Error writing to file");
  22. }
  23. }// end SAVE

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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Can JAVA create DOS directories?

 
0
  #2
Aug 3rd, 2007
this is your problem 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)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

 
0
  #3
Aug 3rd, 2007
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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Can JAVA create DOS directories?

 
0
  #4
Aug 3rd, 2007
Thanks for the tip on relative and absolute references.
I added to your line with this:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

 
0
  #5
Aug 3rd, 2007
  1. public void createFile(String in)
  2. {
  3. toWork = new File(in);
  4. try
  5. {
  6. if(toWork.exists() == false)
  7. {
  8. System.out.println("DNE");
  9. toWork.createNewFile();
  10. }
  11. if(toWork.canRead() == false)
  12. System.out.println("Read error.");
  13. if(toWork.canWrite() == false)
  14. System.out.println("Write error.");
  15. }
  16. catch(Exception e){}
  17. }

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 12:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Can JAVA create DOS directories?

 
0
  #6
Aug 3rd, 2007
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.
  1. // SAVE
  2. private void btnSaveActionPerformed(ActionEvent evt)
  3. {
  4.  
  5. File toWork = new File("C:" + File.separator + "data" + File.separator + "inventory.dat");
  6.  
  7.  
  8. try
  9. {
  10. if(toWork.exists() == false)// if file and directory do not exist
  11. {
  12. toWork.mkdir();// make directory
  13. toWork.createNewFile(); // create file
  14. System.out.println(listModel); // print list to file
  15. }
  16. if(toWork.canRead()== false)
  17. System.out.println("Read Error.");
  18. if(toWork.canWrite() == false)
  19. System.out.println("Write error.");
  20. }
  21. catch (Exception e){}
  22.  
  23.  
  24. }// end SAVE

p.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 1:02 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Can JAVA create DOS directories?

 
0
  #7
Aug 3rd, 2007
No, Java can't create directories under DOS for the very simple reason that Java doesn't work under DOS.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Can JAVA create DOS directories?

 
0
  #8
Aug 3rd, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Can JAVA create DOS directories?

 
0
  #9
Aug 3rd, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Can JAVA create DOS directories?

 
0
  #10
Aug 3rd, 2007
OK, with the fileoutput stream I was able to write the file, but not create the directory.
With the new File statement I am able to create the directory but not write the file.

When I put both in, neither works.

Anyone?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC