Can't write to a file

S

stevesuts

I posted earlier about writing to a file from an ejb and found that you
can't do that so I am now calling a function outside of my bean that I
pass a vector to. I am not even trying to write the vector contents, I
have hard coded a string to be written to the file. I can creat the
directory, create the file, but I can't write to it. Here is my 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();
}

I get a 'true' return from my canWrite function, so I am assuming that
there are no permission problems. Any help is greatly appreciated.
 
R

Robert

PrintWriter is great but the IO errors are covered up. You could try
checkError(). But I'd start out with a different way to write to a file
until you get this problem fixed. The filewriter obj throws
IOExceptions.
 
R

Rhino

I posted earlier about writing to a file from an ejb and found that you
can't do that so I am now calling a function outside of my bean that I
pass a vector to. I am not even trying to write the vector contents, I
have hard coded a string to be written to the file. I can creat the
directory, create the file, but I can't write to it.

What error message are you getting? What exception is being thrown?
Here is my 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();
}

I get a 'true' return from my canWrite function, so I am assuming that
there are no permission problems. Any help is greatly appreciated.
Have you tried stepping through the programmer with a debugger? I don't know
if you are using an IDE but most of these contain debuggers. I use Eclipse
and if I had the same sort of problem you are having, I would step through
the program with the Eclipse debugger to see what the problem might be. This
would enable you to verify that the path of the file is valid, for example.

Rhino
 
S

stevesuts

I see that my path is "c:\\AOSFTP\\aosFtpFile.txt" Why does Java put
the extra backslash in there? Is that normal or is it one too many? I
am assuming that it should be c:\\AOSFTP\aosFtpFile.txt.....right? How
do I go about correcting this? As you can see from my code, it is not a
hardcoded path. I will probably be putting the directory and file name
into a properties file that will be read in at application startup
time.
 
E

enrique

Backslashes have special meaning in Java strings -- remember escape
sequences (e.g. "\n" or "\t")? So if you use a backslash in another
context, you have to do it twice so Java doesn't think you're trying to
escape a character.

Unfortunately, I can't tell you why you can't write your file based on
the info we have here.
 
A

Abrasive Sponge

public static void main(String[] args) {

FileWriter fw = null;
PrintWriter fout = null;

try {
String dirName = ("c:\\AOSFTP");
String aosFileName = ("aosFtpFile.txt");
File aosFileDir = new File(dirName);
File aosFile = new File(dirName, aosFileName);



if(!aosFileDir.exists()) {
aosFileDir.mkdir();
} else if(!aosFileDir.isDirectory()) {
System.out.println("The Directory does not exist");
return;
}
if(!aosFile.exists()) {
aosFile.createNewFile();
}

//moved here
fw = new FileWriter(aosFile); //this was different I
changed this.
fout = new PrintWriter(fw);


System.out.println(" you can " +(aosFile.canWrite()?"
":"not " )+
"write " + fout);
fout.println("Hi there Steve. I made it into the file");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fout != null) fout.close();
if (fw != null) fw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
 
O

Owen Jacobson

I see that my path is "c:\\AOSFTP\\aosFtpFile.txt" Why does Java put the
extra backslash in there? Is that normal or is it one too many? I am
assuming that it should be c:\\AOSFTP\aosFtpFile.txt.....right? How do I
go about correcting this? As you can see from my code, it is not a
hardcoded path. I will probably be putting the directory and file name
into a properties file that will be read in at application startup time.

As a string literal, "c:\\AOSFTP\\aosFtpFile.txt" is correct. If you
execute the following,

public class StringDemo {
private static final string PATH = "c:\\AOSFTP\\aosFtpFile.txt";

public static void main (String[] args) {
System.out.println (PATH);
}
}

you will see that the actual string contains no double backslashes at all.
In a string literal, backslash is used to escape characters to allow entry
of "\n" for a newline. In order to put a \ character in the string
itself, that \ must be escaped somehow so that its special meaning doesn't
apply -- hence the "\\" construction.

If, in place of the try { } catch { } block you have in your original post
(Message <[email protected]>), you
place the following code:

try
{
String dirName = ("c:\\AOSFTP");
String aosFileName = ("aosFtpFile.txt");
File aosFileDir = new File(dirName);
File aosFile = new File(dirName, aosFileName);

// This will create the file if needed and will do
// all the approriate access checks -- don't duplicate
// them!
Writer out = new FileWriter (aosFile);
try {
out.write ("Hello World.\n");
} catch (IOException e) {
System.out.println ("Exception in writing code.");
} finally {
// Ensure closure in both exceptional and normal cases
out.close ();
}
} catch (IOException e) {
System.out.println ("Exception in initialization code.");
e.printStackTrace ();
}

(a) which, if any, of the catch blocks is fired and
(b) what, if anything, appears in the file?

If this works and the file contains Hello World. then the problem is not
with reading or writing files, it's with the layers between the file
writer itself and the code -- probably your use of PrintWriter, if that's
the case.

Try it! Tell us what you find!
 
S

stevesuts

Abrasive, I want to thank you for your help. I tried it out this
morning and it worked! I have been spinning my wheels for a while.
Thanks again.
 
A

Abrasive Sponge

Abrasive, I want to thank you for your help. I tried it out this
morning and it worked! I have been spinning my wheels for a while.
Thanks again.
You bet...I also wanted to mention that the try...catch....finally block
I provided is the best way to close resource for i/o. There are times
that when I didn't close the resource nothing appeared in my file. I
think that's what was happening in your code.

Enjoy. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top