Young87

当前位置:首页 >个人收藏

java将byte[]转成文件的问题

public static void addFile(byte[] bfile, String fileURI) {  
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        File file = null;
        File dir = null;
        try {  
            System.out.println(fileURI.substring(0,fileURI.lastIndexOf("/")));
            dir = new File(fileURI.substring(0,fileURI.lastIndexOf("/")+1).replace("/", "\\"));  
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();
            }
            file = new File(fileURI);
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    } 

如上是一段将byte[]转为文件保存的方法。
需要注意的是对于File的实例化路径使用“/”或者“\\”都是可行的,

如果File实例化用户生成File对象,亦通用可以使用“/”或“\\”

但对于文件目录的判断:dir.isDirectory(),File对象在实例化时却必须使用“\\”,使用“/”则dir.isDirectory()只会返回false.

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 实例演示——————简单演示XSS store攻击,便于学习理解XSS store

下一篇: 编写一个简单的shell

精华推荐