Try this:
private boolean SaveDataToFile(SomeType data)
{
ObjectOutputStream objectOut = null;
try
{
File file = new File("sdcard/" + MY_PACKAGE_NAME);
if(!file.exists() && file.mkdir())
{
file = new File(MY_FILE_NAME);
if(!file.exists())
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(MY_FILE_NAME);
objectOut = new ObjectOutputStream(new BufferedOutputStream(stream));
objectOut.writeObject(data);
return true;
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
finally
{
try
{
if(objectOut != null)
objectOut.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
On Sat, Apr 4, 2009 at 2:09 PM, Mark Murphy <[email protected]> wrote:
>
> javame_android wrote:
> > Hi All,
> >
> > I would like to know how to write to file. I have tried using
> >
> > OutPutStream out = new FileOutPutStream(fileName);
> > out.write(byte [], 0, length);
> >
> > This way always returns Parent directory or File is not writable.
> >
> > I have also used openFileOutPut(fileName, MODE_PRIVATE) method but
> > this method works properly if it is called from the same class which
> > has extended Activity and I am writing to the file in different class
> > and that too in different thread.
> >
> > I have tried to get the Application Context through
> > getApplciationContext and call openFileOutPut method but still theres
> > something wrong that I am doing.
> >
> > So please can someone suggest me a proper way to write to a file.
> >
> > Help me out! Does anyone knows what this error is about and how it can
> > be removed.
>
> Try getFilesDir() in your Activity.
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com | http://twitter.com/commonsguy
>
> _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---