日期:2014-05-20  浏览次数:20789 次

根据byte数组内容生成文本文件?
大家好,现在有一个需求想用java IO实现,程序能得到的输入内容是byte数组的形式,然后要把内容写成一个文本文件。
我现在用的ByteArrayInputStream作为输入流对象,输出流我用的是FileOutputStream,请教大家具体的程序实现应该怎么写呢?谢谢了!
------解决方案--------------------

        byte[] bytes = "fsadf".getBytes();//byte数组
        try {
            OutputStream os = new FileOutputStream(new File("输出文件路径"));                       
            BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(bytes));
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
                os.flush();
            }
            is.close();
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }