file io
Hi there
I am trying to save some text into a text file using FileWriter
and i was wondering if it is possible to specify where to save the txt file in the file system because it saves the file by default in the directory that
contains the class.
I tryed something like
.
.
.
public void save()
{
try
{
saveProfile = new FileWriter("c:/profile.txt");
saveProfile.write(playerName);
saveProfile.close();
}
catch (FileNotFoundException e1)
{
System.out.println("Cannot Create file" + fileName);
}
catch (IOException e2)
{
System.out.println("Error" + e2.toString());
}
finally
{
System.out.println("Profile has been Saved.");
}
}
but it doesn't wotk......any ideas?
thanx!!!!!
When you say "doesn't work", what does that mean?
Instead of printing your own uninformative messages in the catch-clauses, print the message (or the stacktrace) of the exception instead, it should tell you what goes wrong.
For example, on Vista I don't have permission to write just any file in C:\, so I get the message: "c:\profile.txt (Access is denied)"
A file FileNotFoundException is caught so i get the
Cannot Create file c:/profile.txt message......
I also have vista...!?!.....do u think it has to do with read write permissions? but it is in my local drive......i ve used pascal and c and vb for file io in the past and didnt have any problems but in windows xp
Yes, it's a FileNotFoundException with a message "c:\profile.txt (Access is denied)" (which you would be able to see if you printed the message or the stack trace).
Vista is pretty strict about who gets permission to write to C:\, C:\Program Files\ etc.
In any case, writing to fixed paths is a bad idea in general. Best to have a path relative to one of the user-related system properties, like user.home.
Instead of "C:\profile.txt", try System.getProperty("user.home")+File.separator+"myapp"+File.separator+"profile.txt", for a platform-independent, user- and application-specific way to store your data in the home directory of the "current" user (replace "myapp" with the name of your application).
If the problem is that Vista won't allow this sort of thing on the base 'C' directory (I have no idea if that's true and won't look it up right now), then a simple test to get/put the file to some other sub-directory will clear this up, won't it?