繁体   English   中英

在Android中使用外部类对文件进行读写操作?

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

好的,这是问题所在:

我正在尝试做一个Android应用程序(联系应用程序)。 我有3个班级的问题。

  • 主页活动
  • 联系人管理器
  • 联系商店

MainPage是Android应用程序中的MainActivity。 ContactManager是一个类,用于管理应用程序中的所有联系人。 此类接收保存在我创建的列表(SortedList)中并发送到ContactStore类的联系信息。 ContactStore类有两种方法:addToFile和loadFromFile。 此类从文件写入和读取。

ContactManager类与ContactStore之间发送和接收列表。

ContactStore适用于Java,但不适用于android。

我不知道如何在ContactStore中访问android的内部存储或外部存储的位置。

这是我的类的重要方法:

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));

}

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);
}

和ContactStore类:

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();
    }

}

}

我知道我必须提供文件的路径(内部或外部存储),但是我不知道该怎么做,因为总是给我一个错误。 如果您听不懂,请告诉我。 非常感谢,希望您能帮助我。 ;)

外部存储(SD卡):

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

内部存储器:

String root = context.getFilesDir();

因此,您的ContactStore类构造函数(因为在那里需要上下文):

private final File file;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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