PeterMcDonnell
asked on
Problem Accessing a file in Java
I have the following code
private static void WriteStringToFile(String myfilename,String myString,boolean Overwrite){
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
if (f_file.delete()==false){
System.out.println("Deleti ng File "+myfilename+" Failed");
}
}
}
f_file=null;
try{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge tBytes());
f_output.close();
}
catch (IOException e){
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
if i call it once it works ( i am sending a true to the overwrite) if I call it and the file exists the delete fails.
i get:
(The requested operation cannot be performed on a file with a user-mapped section open)
yet I have closed the stream.
I get the same problem if rem out the delete lines
not sure why
can anyone help?
private static void WriteStringToFile(String myfilename,String myString,boolean Overwrite){
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
if (f_file.delete()==false){
System.out.println("Deleti
}
}
}
f_file=null;
try{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge
f_output.close();
}
catch (IOException e){
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
if i call it once it works ( i am sending a true to the overwrite) if I call it and the file exists the delete fails.
i get:
(The requested operation cannot be performed on a file with a user-mapped section open)
yet I have closed the stream.
I get the same problem if rem out the delete lines
not sure why
can anyone help?
private static void WriteStringToFile(String myfilename,String myString,boolean Overwrite){
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
if (f_file.delete()==false){
System.out.println("Deleti ng File "+myfilename+" Failed");
}
}
}
f_file=new File(myfilename);
try{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge tBytes());
f_output.close();
}
catch (IOException e){
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
if (f_file.delete()==false){
System.out.println("Deleti
}
}
}
f_file=new File(myfilename);
try{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge
f_output.close();
}
catch (IOException e){
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
>> f_file=null;
should be
f_file=new File(myfilename);
should be
f_file=new File(myfilename);
Simple thing you can dow with this code is if you don't want to overwrite, then here is the code
private static void WriteStringToFile(String myfilename,String myString,boolean Overwrite){
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
return;
}
}
}
/** Your Other Code Here **/
}
private static void WriteStringToFile(String myfilename,String myString,boolean Overwrite){
File f_file=new File(myfilename);
if (f_file.exists()==true){
if(Overwrite==true){
return;
}
}
}
/** Your Other Code Here **/
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fundamentally I need to delete the file (if it exists ) as the contents are different
I have tried
f_file=new File(myfilename);
this does not work
also if I change:
FileOutputStream f_output=new FileOutputStream(f_file);
to
FileOutputStream f_output=new FileOutputStream(myfilenam e);
even though this then isolates the try code segment this also does not work
the failure appears to be due to an open stream but the stream is closed with the line
f_output.close();
it actually fails on the delete() statement as in it returns false
I have tried
f_file=new File(myfilename);
this does not work
also if I change:
FileOutputStream f_output=new FileOutputStream(f_file);
to
FileOutputStream f_output=new FileOutputStream(myfilenam
even though this then isolates the try code segment this also does not work
the failure appears to be due to an open stream but the stream is closed with the line
f_output.close();
it actually fails on the delete() statement as in it returns false
ASKER
The file is not open anywhere else (I can delete it from the command prompt)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
this also fails with the same error
File IO Error Creating File c:\adobe\fonts\FontList.tx t
java.io.FileNotFoundExcept ion: c:\adobe\fonts\FontList.tx t (The requested operation cannot be performed on a file with a user-mapped section open)
File IO Error Creating File c:\adobe\fonts\FontList.tx
java.io.FileNotFoundExcept
Try to pass the filename something like this
c:/adobe/fonts/FontList.tx t
c:/adobe/fonts/FontList.tx
ASKER
I do pass a fully qualified path as my system.out statement is where this information is received from
try do following at command prompt and then try ur java example
attrib -h -r -s -a c:\adobe
attrib -h -r -s -a c:\adobe\fonts
attrib -h -r -s -a c:\adobe
attrib -h -r -s -a c:\adobe\fonts
Did you already try to reboot your machine and try again.
I know it sounds rather stupid, but it wouldn't be the first time this helps...
I know it sounds rather stupid, but it wouldn't be the first time this helps...
ASKER
have rebooted no luck
is this problem coming from fontlist.txt or any other file that you are trying
Are you able to delete/rename this file from you OS level?
(by calling "del", or "rm", or pressing F8 in Windows Commander, etc.)
(by calling "del", or "rm", or pressing F8 in Windows Commander, etc.)
ASKER
I can delete the file from the same prompt window as i run the java programme
i.e del c:\adobe\fonts\fontlist.tx t
Works
Also I have the same problem on other directory and files
i.e del c:\adobe\fonts\fontlist.tx
Works
Also I have the same problem on other directory and files
I wanted to know one more question from ur side. If u know c/c++
try this
#include "studio.h"
void main()
{
FILE* fp=fopen("c:\\a.txt","w");
if(fp==NULL)
printf("Error in creation file");
}
try this
#include "studio.h"
void main()
{
FILE* fp=fopen("c:\\a.txt","w");
if(fp==NULL)
printf("Error in creation file");
}
I've actually run this example - and it works perfectly on my system... 8-/
Here's the code of my method (just a little modified):
private static void writeStringToFile(String myfilename, String myString, boolean overwrite)
{
File f_file = new File(myfilename);
if (f_file.exists())
{
if (!overwrite)
return;
if (!f_file.delete())
System.out.println("Deleti ng File "+myfilename+" Failed");
}
try
{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge tBytes());
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
Here's the code of my method (just a little modified):
private static void writeStringToFile(String myfilename, String myString, boolean overwrite)
{
File f_file = new File(myfilename);
if (f_file.exists())
{
if (!overwrite)
return;
if (!f_file.delete())
System.out.println("Deleti
}
try
{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
I called it like this:
writeStringToFile("myfile. txt", "myString1", true);
writeStringToFile("myfile. txt", "myString2", true);
writeStringToFile("myfile. txt", "myString3", false);
...and "myfile.txt" contains string "myString2" (as it supposed to be).
writeStringToFile("myfile.
writeStringToFile("myfile.
writeStringToFile("myfile.
...and "myfile.txt" contains string "myString2" (as it supposed to be).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> What is the sense in creating a new file object then testing for its existence may I ask?
The File constructor doesn't create file "physically".
The actual file is created when you call FileOutputStream constructor.
The File constructor doesn't create file "physically".
The actual file is created when you call FileOutputStream constructor.
My point is the logic is wrong.
Oh yeah. You're right. It doesn't make much sense.
Unless it was to be used as the "append to file" indicator...
But in this case this method should look like this:
private static void writeStringToFile(String myfilename, String myString, boolean append)
{
try
{
FileOutputStream f_output=new FileOutputStream(new File(myfilename), append);
f_output.write(myString.ge tBytes());
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
Unless it was to be used as the "append to file" indicator...
But in this case this method should look like this:
private static void writeStringToFile(String myfilename, String myString, boolean append)
{
try
{
FileOutputStream f_output=new FileOutputStream(new File(myfilename), append);
f_output.write(myString.ge
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
Not sure why you put "append" there when he wants to overwrite it, or not write at all.
.
.
.
sm = System.getSecurityManager( );
.
.
.
private static void writeStringToFile2(String myfilename, String myString, boolean overwrite)
{
if (!overwrite){return;}
try{try{
sm.checkRead(myfilename);
}catch(NullPointerExceptio n npe){}
}catch(SecurityException secrtyexcep){return;}
File f_file = new File(myfilename);
try
{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge tBytes());
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
.
.
.
sm = System.getSecurityManager(
.
.
.
private static void writeStringToFile2(String myfilename, String myString, boolean overwrite)
{
if (!overwrite){return;}
try{try{
sm.checkRead(myfilename);
}catch(NullPointerExceptio
}catch(SecurityException secrtyexcep){return;}
File f_file = new File(myfilename);
try
{
FileOutputStream f_output=new FileOutputStream(f_file);
f_output.write(myString.ge
f_output.close();
}
catch (IOException e)
{
System.out.println("File IO Error Creating File "+myfilename+"\r\n"+e);
}
}
And I should have mentioned, for clarity, that if you want to overwrite at all, then you won't need to explicitly delete the file - which is much the same principle as why the method should return if 'overwrite' is false. Perhaps the method should not be called if overwrite is false, since you dont need a file at all in that case. ;)
> Not sure why you put "append" there when he wants to overwrite it, or not write at all.
Because I thought you meant it. :-]
And I still think there's a flaw in design - but on a higher level. Look, why would you design a method which would allow you to write something to file, but do it only if this file doesn't exist? And, assuming that you'd even need something like this, why would you want to pass the "overwrite" parameter to it? The caller of this method should be well aware if this method was already called or not, and it should hold this information as its private variable. If somebody would call this method with "overwrite" parameter set to "false", it would mean that he doesn't know what (wnd why) he's really doing.
PS. And, of course, you're absolutely right about this file checking & deletion.
Because I thought you meant it. :-]
And I still think there's a flaw in design - but on a higher level. Look, why would you design a method which would allow you to write something to file, but do it only if this file doesn't exist? And, assuming that you'd even need something like this, why would you want to pass the "overwrite" parameter to it? The caller of this method should be well aware if this method was already called or not, and it should hold this information as its private variable. If somebody would call this method with "overwrite" parameter set to "false", it would mean that he doesn't know what (wnd why) he's really doing.
PS. And, of course, you're absolutely right about this file checking & deletion.
>> Because I thought you meant it. :-]
Not quite sure what you are getting at there; but anyway, sometimes people ask questions which are not presented in context. The asker wants to write stuff to a file, and if he wants the unnecessary luxury of handling an overwrite argument then that's up to him I suppose - who knows? My code is only trying to fake a different way of establishing if the file already exists or not (which is again a redundant activity), and I wanted to get away from using new File() and then testing for it as I said. The SecurityManager and its NPE on the filename, kludges its way around that. ;) I'm not saying it's the best, or even a good, way. ;)
Not quite sure what you are getting at there; but anyway, sometimes people ask questions which are not presented in context. The asker wants to write stuff to a file, and if he wants the unnecessary luxury of handling an overwrite argument then that's up to him I suppose - who knows? My code is only trying to fake a different way of establishing if the file already exists or not (which is again a redundant activity), and I wanted to get away from using new File() and then testing for it as I said. The SecurityManager and its NPE on the filename, kludges its way around that. ;) I'm not saying it's the best, or even a good, way. ;)
ASKER
The File in question is a summary file which only needs to be overwritten in certain circumstances (when a time or a specific change has occured) the routine needs to write a new text file and overwrite the old one when necessary. The purpose is to isolate this process in one routine for future use. The overwrite parameter also makes it compatable with calling file structure for other file routines (in accordance with a specification).
In conclusion given all the advice the problem must be on the machine and any related code.
Thanks to all
In conclusion given all the advice the problem must be on the machine and any related code.
Thanks to all
8-)
> The overwrite parameter also makes it compatable with calling file structure for other file routines (in accordance with a specification).
I see. That's very pragmatic solution...
Your system architect must be one clever guy, indeed.
I see. That's very pragmatic solution...
Your system architect must be one clever guy, indeed.
>> Your system architect must be one clever guy, indeed. ...
or perhaps just a little bit out of his mind.
or perhaps just a little bit out of his mind.
>> f_output.close();
f_file.close(0;