Android:在应用程序关闭并重新启动后访问数据

Android:在应用程序关闭并重新启动后访问数据,android,Android,我想在我的应用程序第一次启动时创建一个hashmap,并在应用程序从任务管理器关闭后能够访问该hashmap中写入的数据。我该怎么做呢?您有很多选择: “共享偏好 在键值对中存储私有基元数据。 内部存储 在设备内存中存储私有数据。 外部存储 将公共数据存储在共享的外部存储器上。 SQLite数据库 将结构化数据存储在专用数据库中。 网络连接 使用您自己的网络服务器在web上存储数据。“ 因为它是一个映射,所以最容易将其写入文本文件,然后使用某种密钥系统(可能是JSON或xml)对其进行“重新编译

我想在我的应用程序第一次启动时创建一个hashmap,并在应用程序从任务管理器关闭后能够访问该hashmap中写入的数据。我该怎么做呢?

您有很多选择:

“共享偏好 在键值对中存储私有基元数据。 内部存储 在设备内存中存储私有数据。 外部存储 将公共数据存储在共享的外部存储器上。 SQLite数据库 将结构化数据存储在专用数据库中。 网络连接 使用您自己的网络服务器在web上存储数据。“

因为它是一个映射,所以最容易将其写入文本文件,然后使用某种密钥系统(可能是JSON或xml)对其进行“重新编译”

请在此处阅读更多信息:

编辑地图的文件保护程序。您必须根据自己的需要对其进行编辑

final public class FileHandler {
    final private File folderCreatedDir;
    final private String folderToCreate;
    final private Map<String, String> mapNameToContents;

    public FileHandler(File baseDirectory, String folderToCreate,
            Map<String, String> mapNameToContents) {
        this.folderCreatedDir = new File(baseDirectory + File.separator
                + folderToCreate);
        this.folderToCreate = folderToCreate;
        this.mapNameToContents = mapNameToContents;
    }

    private final void createFolder() {
        folderCreatedDir.mkdir();

    }

    private final void writeMapContents() throws IOException {
        Set<String> keySet = mapNameToContents.keySet();
        for (String key : keySet) {
            writeContents(key, mapNameToContents.get(key));
        }

    }

    private final void writeContents(String key, String contents)
            throws IOException {
        File file = new File(folderCreatedDir + File.separator + key);
        FileOutputStream fileOutput = new FileOutputStream(file);
        if (file.canWrite()) {
            fileOutput.write(contents.getBytes());
            fileOutput.close();
        }
    }

    public void writeAllContents() throws IOException {
        createFolder();
        writeMapContents();
    }

    public StringBuilder getContents(String key) throws IOException {
        BufferedReader rd = new BufferedReader(new FileReader(folderCreatedDir
                + File.separator + key));
        String line = "";
        StringBuilder htmlBuilder = new StringBuilder();
        long bytesRead = 0;
        while ((line = rd.readLine()) != null) {
            htmlBuilder.append(line);
            bytesRead = bytesRead + line.getBytes().length + 2;
        }
        return htmlBuilder;
    }
}
final公共类文件处理程序{
最终私有文件folderCreatedDir;
最终私有字符串folderToCreate;
最终私有地图mapNameToContents;
公共文件处理程序(文件基目录、字符串folderToCreate、,
地图名称(地图内容){
this.folderCreatedDir=新文件(baseDirectory+File.separator
+folderToCreate);
this.folderToCreate=folderToCreate;
this.mapNameToContents=mapNameToContents;
}
私有最终void createFolder(){
folderCreatedDir.mkdir();
}
private final void writeMapContents()引发IOException{
设置keySet=mapNameToContents.keySet();
用于(字符串键:键集){
writeContents(key,mapNameToContents.get(key));
}
}
私有最终void writeContents(字符串键、字符串内容)
抛出IOException{
File File=新文件(folderCreatedDir+File.separator+key);
FileOutputStream fileOutput=新的FileOutputStream(文件);
if(file.canWrite()){
fileOutput.write(contents.getBytes());
fileOutput.close();
}
}
public void writealcontents()引发IOException{
createFolder();
writeMapContents();
}
公共StringBuilder getContents(字符串键)引发IOException{
BufferedReader rd=新的BufferedReader(新文件读取器(folderCreatedDir
+File.separator+key));
字符串行=”;
StringBuilder htmlBuilder=新StringBuilder();
长字节读取=0;
而((line=rd.readLine())!=null){
htmlBuilder.append(行);
bytesRead=bytesRead+line.getBytes().length+2;
}
返回htmlBuilder;
}
}

保存文件时如何覆盖文件?它会自动覆盖吗?@user2624018是。这是一个地图的文件保护程序。你必须稍微修改一下。