【问题标题】:How can I write an Object to my raw folder file如何将对象写入原始文件夹文件
【发布时间】:2016-12-02 15:30:21
【问题描述】:

这是我的可序列化类。

public class Person implements Serializable{
private String name;
private String surname;
private int age;


public Person(String name,String surname,int age){
    this.name = name;
    this.surname = surname;
    this.age = age;
}

public void setSurname(String surname) {

    this.surname = surname;
}

public void setName(String name) {
    this.name = name;
}
public void setAge(int age){
    this.age = age;
}

public int getAge() {
    return age;
}

public String getName() {
    return name;
}

public String getSurname() {
    return surname;
}
public String toString(){
return name + " "+surname+" "+age;
}
}

我正在尝试使用 FileOutputStream 将 Person 类写入原始文件夹,但没有任何效果。无法创建 FileOutputStream 来获取原始文件夹文件。

我还尝试了 FileOutputStream。

 FileOutputStream fos = getBaseContext().openFileOutput(
     "android.resource://com.cpt.sample/raw/text.txt",MODE_PRIVATE);

【问题讨论】:

  • 为什么要写在raw/文件夹里面?
  • “res”文件夹中的任何数据都是不可写的。更多细节在这里:stackoverflow.com/questions/10618511/…
  • 那么我可以在哪里保存文件以供以后使用? @m0skit0
  • 那么我应该把文件放在哪里? @MiguelBenitez
  • 我认为@Dharvik shah 已经给了你正确的答案:)

标签: java android serialization fileinputstream fileoutputstream


【解决方案1】:

您不能更改资源文件。它们在运行时是静态的。你可以查看here

并在运行时放置文件,您可以使用:

  • 内部存储(或)
  • 外部存储

要获取此目录,请使用:Environment.getExternalFilesDir()

更多信息请到here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多