首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

可不可以将 byte[] 转为 File

2011-12-11 
能否将 byte[]转为 File?我的代码里byte[] filecontent decoder.decodeBuffer(body)解码出一个byte[],

能否将 byte[] 转为 File?
我的代码里
byte[] filecontent = decoder.decodeBuffer(body);解码出一个byte[],这个是个文件类型,我想转化回去File应该怎么做啊

File testFile = new File(filecontent....);

诸如此类的 谢谢

[解决办法]
应该不可以吧,File类没有这么的构造函数(这个你可以参考JDOC或API或源代码),我看过是没有这样的构造函数的
不过你可以类似下面这样先建一个,然后把你的bytes写进去,用完后再删掉

Java code
byte[] byteArray={0x59,0x34,0x45};        File f = new File("temp.te");        if(!f.exists())        {            try {                f.createNewFile();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        FileOutputStream fos;        try {            fos = new FileOutputStream(f);            fos.write(byteArray);            fos.close();                        //do something you want;                        f.delete();//delete it after using it        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }
[解决办法]
探讨
out 这个FileOutputStream 又怎么最终转化为File

[解决办法]
楼主到底要保存什么呢?

一直没搞明白

要不你就ObjectInputStream/ObjectOutputStream

按对象序列化来

热点排行