Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileWriter Wont Write To Directory

Status
Not open for further replies.

bobrivers2003

Technical User
Oct 28, 2005
96
GB
I wish to save a file if i put c:\\file.txt, the file is saved in the c: drive, however if i put c:\\dir\\file.txt it doesnt work. What am I doing wrong? (I am working on Windows 2000)



try {
out = new BufferedWriter(new FileWriter("c:\\file.txt, true));
out.write(new Date() + " : " + lm + '\n');
out.close();

} catch (Exception e) {

if (out != null) {

try {
out.close();

} catch (Exception er) {

}

}
}

 
...and the C:\dir directory definitely exists?

Tim
 
Ahh, no it doesnt and works a treat when it is there. My low point of today!!!

Is there a way of creating the directory if it does not exist?

Thanks :)
 
Have a read of the File javadoc ...

Code:
File dir = new File("C:/dir");
if (!dir.exists()) {
  dir.mkdir();
}

dir = new File("C:/dir");
if (!dir.exists()) {
   throw new IOException("Could not make dir : " +dir);
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Look at the mkdir method of the File class.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top