把字符串字节数组写入文件

    /**
     * Java,把字符串字节数组写入文件
     * @param byt
     * @throws Exception
     */
    private static void byte2File(byte[] byt) throws Exception {
        if (null == byt) {
            return;
        }
        String targetFile = "C:\\Users\\fileTestTarget.txt";
        File file = new File(targetFile);
        OutputStream output = new FileOutputStream(file);
        BufferedOutputStream out = new BufferedOutputStream(output);
        InputStream in = new ByteArrayInputStream(byt);
        byte[] buff = new byte[1024];
        int len = 0;
        while ((len = in.read(buff)) != -1) {
            out.write(buff, 0, len);
        }
        in.close();
        out.close();
        System.out.println("---- done ----");
    }

       如果入参是字符串“HelloWorld”的字节数组,则可以吧HellowWorld写入fileTestTarget.txt。

posted @ 2020-01-18 07:16  楼兰胡杨  阅读(1601)  评论(0编辑  收藏  举报