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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't write to a file...

Status
Not open for further replies.

tpman

Programmer
Oct 15, 2004
25
US
I have been trying to figure out why I can't see anything written to a file. I can create the directory, create the file, but when it comes to writing anything to it, I can't.

Here is the code:
try
{
String dirName = ("c:\\AOSFTP");
String aosFileName = ("aosFtpFile.txt");
File aosFileDir = new File(dirName);
File aosFile = new File(dirName, aosFileName);
PrintWriter fout = new PrintWriter(new FileWriter(aosFileName));


if(!aosFileDir.exists())
{
aosFileDir.mkdir();
}
else if(!aosFileDir.isDirectory())
{
System.out.println("The Directory does not exist");
return(1);
}
if(!aosFile.exists())
{
aosFile.createNewFile();
}
System.out.println(" you can " +(aosFile.canWrite()?" ":"not " )+ "write " + fout);
fout.println("Hi there Steve. I made it into the file");
fout.close();


}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

The canWrite function returns 'true', so I am assuming that there are no permission issues stopping me. Any help is greatly appreciated..
 
Just construct the PrintWriter as

Code:
PrintWriter fout = new PrintWriter(new FileOutputStream(aosFile));

Btw, this is a J2EE forum, for pure Java questions, use the Java forum.

Cheers,

Dian
 
Well first off, you are creating the PrintWriter, and then checking that the dir/file exists ... that seems like the wrong way around to me.

And yes, please use form269 for future core Java API questions.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top