944,218 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8002
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 3rd, 2007
0

Can JAVA create DOS directories?

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

this is your problem out = new FileOutputStream("inventory.dat");.
Try this and see what happends out = new FileOutputStream("C:" + File.separator + "inventory.dat");
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

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)
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

Thanks for the tip on relative and absolute references.
I added to your line with this:
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

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.
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

No, Java can't create directories under DOS for the very simple reason that Java doesn't work under DOS.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Can JAVA create DOS directories?

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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Unicode, CSV
Next Thread in Java Forum Timeline: Convert User Supplied Int to App Generated





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC