简体   繁体   中英

Write/Read from file with an external class in Android?

OK this is the problem:

I am trying to do an Android application (Contact application). I have problem with 3 classes.

  • MainPage Activity
  • ContactManager
  • ContactStore

The MainPage is the MainActivity in the Android application. ContactManager is a class that manage all the contacts in the application. This class receive the contact information that save in a List that I created (SortedList) and send to the ContactStore class. The ContactStore class have two methods: addToFile and loadFromFile. This class write and read from a file.

The ContactManager class send and receive a list to and from the ContactStore.

The ContactStore works for java, but for android don't work.

I don't know how to access the location of the internal storage or external storage of android in the ContactStore.

Here is my classes with the important methods:

public class MainPage extends ListActivity {

private static ContactManager contactManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_page);


    contactManager = new ContactManager(this);

    setListAdapter(new MainPageAdapter(this));

}

The ContactManager:

public class ContactManager {


private SortedList<Contact> contactList = new SortedArrayList<Contact>();

private ContactStore contactStore;

public ContactManager(Context context){
    super();
    this.contactStore = new ContactStore(context);
    this.readContacts();
}

/**
 * Add to ContactsList
 * @param firstName
 * @param lastName
 * @param cellPhone
 * @param workPhone
 * @param email
 */
public void addContact(String firstName, String lastName, String cellPhone, String workPhone, String email){
    contactList.add(new Contact(firstName, lastName, cellPhone, workPhone, email));

    contactStore.addToFile(this.contactList);
}

/**
 * Read from contact list
 * @return the contact list
 * @throws FileNotFoundException 
 */
public void readContacts(){
    this.contactList.clear();

    contactStore.loadFromFile(this);
}

And the ContactStore class:

public class ContactStore {

private final File file = new File("Contacts.txt");


public ContactStore(Context context) {
    super();

}


public void addToFile(SortedList<Contact> contactList){

    try {

        file.createNewFile();

        PrintWriter out = new PrintWriter(file);

        try{
            for(int i=0;i<contactList.size();i++){
                out.println(contactList.get(i).toString());
            }
        }
        finally{
            out.close();
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void loadFromFile(ContactManager contactManager){

    try {
        Scanner in = new Scanner(file);
        try{
            while(in.hasNextLine()){
                String line = in.nextLine();
                String[] tokens = line.split(" ");
                contactManager.addContact(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);

            }
        }
        finally{
            in.close();
        }

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

}

}

I know that I have to give the path(internal or external storage) to the file, but I don't know how to do that, because always give me an error. If you don't understand something please let me know. Thanks a lot, and I hope that you can help me. ;)

External storage (sdcard):

String root = Environment.getExternalStorageDirectory().getAbsolutePath();

Internal storage:

String root = context.getFilesDir();

thus your ContactStore class constructor (because you need the context in there):

private final File file;

public ContactStore(Context context) {
  super();
  String root = context.getFilesDir();
  file = new File(root + File.separator + "Contacts.txt");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM