当前位置: 代码迷 >> J2SE >> 可不可以将 byte[] 转为 File
  详细解决方案

可不可以将 byte[] 转为 File

热度:316   发布时间:2016-04-24 13:02:00.0
能否将 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

按对象序列化来

  相关解决方案