美文网首页
file 外部存储

file 外部存储

作者: _弓长_大人 | 来源:发表于2018-09-25 12:43 被阅读7次

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
ArrayList<Person> persons;
ArrayAdapter<Person> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    persons = new ArrayList<>();
    restore();

    ListView listView = findViewById(R.id.listView);
    arrayAdapter = new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,persons);
    listView.setAdapter(arrayAdapter);

//
// if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
// ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
// }
}

public void add(View v){
    EditText editText_name = findViewById(R.id.editText_name);
    EditText editText_age = findViewById(R.id.editText_age);

    Person person = new Person(editText_name.getText().toString(),Integer.parseInt(editText_age.getText().toString()));
    persons.add(person);
    arrayAdapter.notifyDataSetChanged();

}

@Override
protected void onPause() {
    super.onPause();
    save();
}

//外部存储
public void save(){
    try {

// File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Log.d(TAG, "save: "+file.getAbsolutePath());
FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsolutePath()+"/persons");
// FileOutputStream fileOutputStream = openFileOutput("persons",MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

        objectOutputStream.writeObject(persons);

        objectOutputStream.close();
        fileOutputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void restore(){
    try {

// File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
FileInputStream fileInputStream = new FileInputStream(file.getAbsolutePath()+"/persons");

// FileInputStream fileInputStream = openFileInput("persons");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

        persons = (ArrayList<Person>) objectInputStream.readObject();

        fileInputStream.close();
        objectInputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

// public void save(){
// try {
// FileOutputStream fileOutputStream = openFileOutput("persons",MODE_PRIVATE);
// ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//
// objectOutputStream.writeObject(persons);
//
// objectOutputStream.close();
// fileOutputStream.close();
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// public void restore(){
// try {
// FileInputStream fileInputStream = openFileInput("persons");
// ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//
// persons = (ArrayList<Person>) objectInputStream.readObject();
//
// fileInputStream.close();
// objectInputStream.close();
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// }
}

package cn.edu.sicnu.persondemo;

import java.io.Serializable;

/**

  • Created by liguiyang on 2018/4/24.
    */

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

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

public String getName() {
    return name;
}

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

public int getAge() {
    return age;
}

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

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
}

}

相关文章

  • file 外部存储

    public class MainActivity extends AppCompatActivity { ///...

  • Android 中的数据存储

    分类 共享参数存储 File存储(内部,外部) SQLite数据库 Content Provider 网络存储 1...

  • 网络与数据存储之管理文件

    存储在内部还是外部?*Internal storage(内部)** getFileDir():返回一个File,代...

  • java.io.File 类

    java 中引入 File 类的目的就是为了将一个外部文件系统中的存储数据的文件和 Java 语言中的 File ...

  • android相关本地存储目录

    android本地存储目录 内部存储 外部存储应用私有目录外部目录 获得内部存储目录 获得外部存储目录 获得外部存...

  • 深入理解Android中的缓存机制(三)磁盘缓存

    概述 磁盘存储有两种形式,一种是File存储,一种是DB(DataBase)存储。 File File存储比较常见...

  • 深入理解Android缓存机制(三)磁盘缓存

    概述 磁盘存储有两种形式,一种是File存储,一种是DB(DataBase)存储。 File File存储比较常见...

  • File存储

    Context提供以下方法操作File存储 1.FileInputStream fis = openFileInp...

  • android存储

    android手机分为外部存储和内部存储,外部存储是否存在因手机而异,可通过代码判断是否存在外部存储。 网上看到关...

  • File存储(内部存储)

    一旦程序在设备安装后,data/data/包名/ 即为内部存储空间,对外保密。Context提供了2个方法来打开输...

网友评论

      本文标题:file 外部存储

      本文链接:https://www.haomeiwen.com/subject/gaxsdftx.html