本文介绍了如何编写一个文件的字节,而不覆盖它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自服务器的字节块,我需要把它们写在一个文件,而不会覆盖旧的块。我该怎么办呢?
我这样做是这样..

 私人无效createMapFile(字符串文件名,字节mapBytes [])抛出IOException异常
    {
        文件文件路径=新的文件(Environment.getExternalStorageDirectory()+/图/+文件名);        PrintWriter的PW =的新PrintWriter(新的FileOutputStream(文件路径,真实));
        pw.print(mapBytes);
        pw.flush();
        pw.close(); }

我的这种方法将文件名和字节作为参数的块。每当我从服务器获取新的块时调用此方法。通过这种方法,我可以不写的所有数据块在文件中。

任何帮助将AP preciative。
谢谢


解决方案

的PrintWriter用于在文件中输入文字,并且不支持写入原始字节

。 的FileOutputStream 提供你需要的一切:

 的FileOutputStream OS =新的FileOutputStream(文件路径,真实);
os.write(mapBytes);
os.close();

i am getting chunks of bytes from server and i need to write them in a file without overwriting the old chunk. how can i do that? i am doing it this way..

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(); }

my this method takes the file name and chunk of bytes as argument. anytime i get new chunk from server this method gets called. by this method i cannot write the all chunks in a file.

any help will be appreciative.thanks

解决方案

PrintWriter is used for writing text in files and doesn't support writing raw bytes. FileOutputStream provides everything you need:

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

这篇关于如何编写一个文件的字节,而不覆盖它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 14:06