android - 如何在文件中写入字节而不覆盖它

标签 android file io

我正在从服务器获取字节 block ,我需要将它们写入文件而不覆盖旧 block 。我怎样才能做到这一点? 我是这样做的..

private void createMapFile(String fileName, byte mapBytes[]) throws IOException
    {       
        File filePath = new File(Environment.getExternalStorageDirectory()+"/map/"+fileName);

        PrintWriter pw = new PrintWriter(new FileOutputStream(filePath, true));  
        pw.print(mapBytes);  
        pw.flush();  
        pw.close(); }

我的这个方法将文件名和字节 block 作为参数。每当我从服务器获取新 block 时,都会调用此方法。通过这种方法,我无法将所有 block 写入文件。

任何帮助将不胜感激。 谢谢

最佳答案

PrintWriter 用于在文件中写入文本,不支持写入原始字节。 FileOutputStream提供您需要的一切:

FileOutputStream os = new FileOutputStream(filePath, true);
os.write(mapBytes);
os.close();

关于android - 如何在文件中写入字节而不覆盖它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568940/

相关文章:

bash - 如何递归遍历目录树并仅查找文件?

java - 查找算术级数中缺失的项 -

haskell - 如何提高Haskell IO的性能?

java - 将加密/解密 key 存储到静态变量中是否安全?

用于大/小字体的 Android 资源限定符(可访问性)

java - 使用java读取 "ParseFile"

Python 不从文件读取

android - 如何将一个 ImageView 拖放到另一个 ImageView 上?

android - 更改 Cordova 项目 android 包名称

c# - C#检查文件的最后修改日期