| | |
Can JAVA create DOS directories?
Thread 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.
Java Syntax (Toggle Plain Text)
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.
I'm no ones son, unforgiven.
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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:
Java Syntax (Toggle Plain Text)
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.
Java Syntax (Toggle Plain Text)
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 12:02 pm.
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.
Java Syntax (Toggle Plain Text)
// 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 1:02 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Unicode, CSV
- Next Thread: Convert User Supplied Int to App Generated
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth bold business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error file formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie openjavafx parameter php problem program programming project recursion repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






