User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Can JAVA create DOS directories?

  #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.
	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 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Can JAVA create DOS directories?

  #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)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jul 2007
Location: Virginia
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #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  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #4  
Aug 3rd, 2007
Thanks for the tip on relative and absolute references.
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.
Reply With Quote  
Join Date: Jul 2007
Location: Virginia
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #5  
Aug 3rd, 2007
    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.
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #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.
// 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 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 12:02 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Can JAVA create DOS directories?

  #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  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #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  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #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  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Can JAVA create DOS directories?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 5:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC