/** * 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。
来源:https://www.cnblogs.com/east7/p/12208116.html