Android中保存int/byte/string数据类型

发布于 2024-12-06 11:33:50 字数 523 浏览 4 评论 0原文

抱歉,这可能是一个很长的问题,但是我如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-12-13 11:33:51

要保存对象,您可以使用 ObjectOutput 类来序列化它们,例如:

// Example object which implements Serializable 
Example example = new Example();

// Save an object 
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( new Integer( YOUR_INT ) );
out.close();


// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
Integer example_loaded = (Integer) in.readObject();
in.close();

其中类示例可以是实现可序列化或字节数据的任何对象,javadocs 也可能有帮助!

希望这有帮助!

To save your objects you can use the ObjectOutput class to serialize them, e.g.:

// Example object which implements Serializable 
Example example = new Example();

// Save an object 
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( new Integer( YOUR_INT ) );
out.close();


// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
Integer example_loaded = (Integer) in.readObject();
in.close();

Where the class Example could be any object that implements serializable or byte data, the javadocs may also help!

Hope this helps!

夏至、离别 2024-12-13 11:33:51

如果您想读取对象,那么您可以从您获得的 FileInputStream 创建一个 ObjectInputStream 。如果您想以二进制方式读取文件,则可以使用与 byte[] 配合使用的 read 成员函数。

If you want to read objects, then you would create an ObjectInputStream from the FileInputStream you got. If you want to read the file as binary, then you can use the read member function that works with a byte[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文