Reputation: 188
I tried this:
public class People implements Serializable {
String name;
public People(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void saveAsObject(People p) throws FileNotFoundException, IOException
{
try
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("test","test.dat") ));
os.writeObject(p);
os.close();
System.out.println("Sucess");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
I created that folder manually. It throws this eror: "java.io.FileNotFoundException: /test/test.txt: open failed: ENOENT" (No such file or directory)
When i tried
new FileOutputStream("text.dat")
it throws java.io.FileNotFoundException: EROFS (Read only file system)
This is my main class:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
People p = new People("Ray");
try {
p.saveAsObject(p);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 1
Views: 3736
Reputation: 2185
Your app's default current working directory is "/", which you won't have permission to write to.
Upvotes: 0
Reputation: 39856
this new File("test","test.dat")
is not a valid way of getting a file path on Android.
try this:
new File(Environment.getExternalStorageDirectory(), "/data.dat");
Upvotes: 1