美文网首页
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 外部存储

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