android - 文件输出流格式化?

标签 android fileoutputstream

我正在构建我的第一个 Android 应用程序,我正在收集 3 个简单的数据字段,然后我会将它们通过电子邮件发送给用户。

我已启动并运行该应用程序,我可以收集数据并将其显示在屏幕上,我还可以通过电子邮件发送数据。但是数据只是放在一行带有已知空格的文本中。

我正在使用 FileOutputStream 写入数据,并且我已将一些文本添加到流中以分解数据..但这似乎不是最佳途径?我应该做什么?下面是一个示例,其中包含我的两个字段和一个静态文本字符串。

它像球一样出来:22 我希望它有空格或标题。 我想我可以添加 fos.write("".getBytes());腾出空间?

谢谢

  FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE);

                fos.write("Balls: ".getBytes());
                fos.write(balls.getBytes());
                fos.write(strikes.getBytes());
                fos.close(); 

最佳答案

为什么不先构造字符串然后调用 getBytes() 处理结果字符串并将其传递给 fos.write()?

例子:

FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE);
String bigstring = "Balls: " + balls + " Strikes: " + strikes;
            fos.write(bigstring.getBytes());
            fos.close();

如果你想在字符串中插入一个换行符,你可以这样做by concatenating a new-line character进入其中。

关于android - 文件输出流格式化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42912125/

相关文章:

java - 如何使用 FileOutputStream 删除 .txt 文件中的一行?

java - 如何检测java中FileCopying的读取错误?

android - 为什么我在图库中看不到我拍摄的照片?

android - FileOutputStream wit 数据库文件在 Android 9 上不再工作

android - Mobile Vision API new detector frame 获取 Bitmap Image

android - FFmpeg 错误 : Unable to find a suitable output format for ''

android - 操作栏,如果单击某个选项卡如何隐藏操作栏(该选项卡单击全屏)

android - 即使未调用 onCreateView 也刷新当前 Fragment

java - StrictMode Activity 实例计数违规(2 个实例,1 个预期)在完全空 Activity 轮换时

java - 文件传输服务器 <-> 客户端,文件不完整