我正在从服务器获取字节 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/