- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我的应用程序启动时,它会连接到数据库并下载一些 XML 格式的数据。这是布局:
<Customers>
<Customer>
<name>...</name>
...
</Customer>
<Customer>
<name>...</name>
...
</Customer>
...
</Customer>
我为每个客户创建了一个类 Customer
的对象,并将其存储在 ArrayList 中。用户可以编辑和添加每个客户的一些数据,他们可以将其上传回服务器。
问题是我不知道在本地存储数据的最佳方式是什么,所以如果我的应用程序关闭或用户不再有互联网,他们会有一个备份。我现在将所有 Customer
对象转换回 XML,然后将其存储在 SD 卡上,但我认为这不是一个好的解决方案。每个客户有大约 40 个字符串、10 个整数和一些我必须存储的 bool 值。有没有更好的方法将数据存储在本地?
我找到了 some code它可以存储一个 ArrayList 但它不适用于自定义类。
编辑:我使用了this tutorial解决我的问题。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import android.os.Environment;
public class SerializeData {
public static byte[] serializeObject(Object o) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch (IOException ioe) {
CreateLog.addToLog(ioe.toString());
return null;
}
}
public static Object deserializeObject(byte[] b) {
try {
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(b));
Object object = in.readObject();
in.close();
return object;
} catch (ClassNotFoundException cnfe) {
CreateLog.addToLog(cnfe.toString());
return null;
} catch (IOException ioe) {
CreateLog.addToLog(ioe.toString());
return null;
}
}
public static void saveData(){
byte[] arrayData = SerializeData
.serializeObject(MyTasks.allCustomers);
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(
Environment.getExternalStorageDirectory()
+ "/myprogram/myarray.txt"));
bos.write(arrayData);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
CreateLog.addToLog(e.toString());
} catch (IOException e) {
CreateLog.addToLog(e.toString());
}
}
public static ArrayList<Customer> getData(){
File afile = new File(Environment.getExternalStorageDirectory()
+ "/myprogram/myarray.txt");
int size = (int) afile.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
CreateLog.addToLog(e.toString());
} catch (IOException e) {
CreateLog.addToLog(e.toString());
}
return (ArrayList<Customer>) SerializeData.deserializeObject(bytes);
}
}
最佳答案
据我了解,Customer 结构将仅在本地读取和写入,并且也由相同或高度相关的程序读取和写入。对于这种情况,我认为 Java 序列化是合适的,因为它添加的代码很少。
让 Customer 实现 Serializable(立即输入 serialVersionUID,以便稍后控制版本)。 ArrayList 已经是可序列化的。按照说明获取文件名here并使用 ObjectInput/Output 流进行序列化。如果需要,您以后可以随时迁移到 XML/JSON。可以找到一个简单的序列化“hello world”示例 here .
关于android - 将 ArrayList 写入 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304863/
在我的代码中,我想以编程方式选择一些变量,并以硬编码方式选择和重命名其他一些变量。我知道我可以通过 setnames() 分两步实现这一点。 ,但我很好奇如何一步完成。 我想我很接近它通过 .SDco
(添加了可重现的示例。) 我对 rnorm 函数有点困惑。 我期待 mean(rnorm(100,mean=0,sd=1))为0;和 sd(rnorm(100,mean=0,sd=1))为 1。但给出
我想创建一个包含多个不同列的数据框,其中包含平均值,之后 sd 显示在括号中。举个例子: df % group_by(Species) %>% summarise_all(list(~ s
我很想知道 SD 卡是否提供类似于“SMART”信息的内容,例如硬盘和 SSD。 我有兴趣在 Raspberry PI 中检查 SD 卡的健康信息,以进行预防性更换。我的写入需求非常高,对设备的物理访
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
是否可以通过编程将 sd 卡中存在的文件夹复制到存在同一 sd 卡的另一个文件夹?? 如果是这样,该怎么做? 最佳答案 该示例的改进版本: // If targetLocation does not
Link :I worked on based on this Link 我添加了这一行来查找尺寸(内部和外部)尺寸, return availableExternalMemorySize/(1024
我正在开发一个应用程序,其中我需要从 sd 卡 中选择一个图像并在 ImageView 中显示它。现在我希望用户通过单击一个按钮来减小/增加其宽度,然后将其保存回 sd 卡。 我已经完成了图像挑选并在
在我的应用程序中,我使用以下 Intent 获得了 SD 卡写入权限。如果用户从系统文件资源管理器中选择 sd 卡文件夹,那么我就有 sd 卡写权限。 Intent intent = new Inte
给定一个data.table library(data.table) DT = data.table(x=rep(c("b","a","c"),each=3), v=c(1,1,1,2,2,1,1,2
我正在构建一个程序,该程序对 pin0 上的模拟电压进行 10 次测量,并将其打印到日志文件中。当我尝试确保文件为空时,我遇到了这个问题。我正在使用 SD.remove() 来删除以前的日志文件。当我
在 Android 的 API > 19 中是否有任何方法可以获取可移动 SD 卡的路径? 与外部 SD 卡一样,我们有 Environment.getExternalStorageDirectory
我使用以下方法检查手机是否包含 SD 卡,但如果 SD 卡不可用,问题总是返回 true,请帮助我。 Boolean isSDPresent = android.os.Environment.getE
这是我将 512 字节块写入 SD 卡的代码。代码工作正常,但是当我检查一切正常时(通过阅读 SD 的响应),我读到 0xFF . 该值应该类似于(来自 SD 引用手册): ‘010’—Data ac
我有两个线程在 epoll 上运行。一个线程尝试与服务器建立 TCP 连接,使用 EPOLL_CTL_ADD 选项将套接字添加到 epoll-fd。 另一个线程负责等待添加到 epoll-fd 的 S
我正在使用 eclipse 模拟器,我想以编程方式将一些 mp3 从 /sdcard/songs 复制到 /sdcard/backup,有什么办法吗?非常感谢任何帮助和代码 fragment !谢谢!
我正在使用 Docker Desktop for Mac 版本 2.1.0.4。我有一个 Docker 容器,它是一个 Ubuntu 18.04 Linux VM,里面有 Yocto Build 系统
好的,这个黑莓应用程序在第一次安装和运行时创建了一个数据库。它安装在 SD 卡上。 当我删除应用程序时 - 这个文件仍然存在,我在删除应用程序时找不到任何删除它的方法。 有什么建议么? 最佳答案 应用
我有一张 SD 卡(或 SDHC 卡)通过 SPI 模式连接到微 Controller 。我正在使用 Chan’s FAT图书馆。我将来自 8192 字节缓冲区的数据写入其中(由于 RAM 不足,缓冲
我想在删除该选择中的最小值和最大值后,计算数据框中每一行在该选择列上的标准差。这是一个例子: set.seed(1) dat dat X1 X2 X3 X4 X5 sd 1 27 5
我是一名优秀的程序员,十分优秀!