Android中保存int/byte/string数据类型
抱歉,这可能是一个很长的问题,但是我如何在 Android 中实际保存 int/byte/string 数据类型?
我确实知道要将字符串保存到内部存储器中(注意,不是外部存储器或其他存储器),我必须这样做:
字符串FILENAME =“hello_file”;
字符串字符串=“你好世界!”;
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
保存文件,并且:
int saveTest = fos.read();
fos.close();
从另一个活动或其他活动中读取文件。 (这是正确的吗?)
但是如果我想将文件保存并读取为 int/byte 数据文件怎么办?这可能吗?我怎样才能做到呢?
Sorry, this might be quite a lengthy question, but how do i actually save int/byte/string data types in Android?
I do know that to save a String into the internal memory (note, not external or anything else), i have to do something like this:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
to save the file, and:
int saveTest = fos.read();
fos.close();
to read the file from another activity or something. (Is this correct?)
But what if i want to save and read the file as an int/byte data file? Is this possible? And how would i be able to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要保存对象,您可以使用 ObjectOutput 类来序列化它们,例如:
其中类示例可以是实现可序列化或字节数据的任何对象,javadocs 也可能有帮助!
希望这有帮助!
To save your objects you can use the ObjectOutput class to serialize them, e.g.:
Where the class Example could be any object that implements serializable or byte data, the javadocs may also help!
Hope this helps!
如果您想读取对象,那么您可以从您获得的
FileInputStream
创建一个ObjectInputStream
。如果您想以二进制方式读取文件,则可以使用与byte[]
配合使用的read
成员函数。If you want to read objects, then you would create an
ObjectInputStream
from theFileInputStream
you got. If you want to read the file as binary, then you can use theread
member function that works with abyte[]
.