我怎样才能写一个字节数组到Java中的文件?

如何写一个字节数组到Java中的文件?

您可以使用Apache Commons IO的 IOUtils.write(byte [] data,OutputStream输出) 。

KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = kgen.generateKey(); byte[] encoded = key.getEncoded(); FileOutputStream output = new FileOutputStream(new File("target-file")); IOUtils.write(encoded, output); 

正如Sebastian Redl现在指出的最直接的java.nio.file.Files.write 。 有关详细信息,请参阅“ 读取,写入和创build文件”教程 。


旧的回答: FileOutputStream.write(byte [])将是最直接的。 你想写什么数据?

Java IO系统教程可能对您有些用处。

从Java 1.7开始,有一个新的方法: java.nio.file.Files.write

 import java.nio.file.Files; import java.nio.file.Paths; KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = kgen.generateKey(); byte[] encoded = key.getEncoded(); Files.write(Paths.get("target-file"), encoded); 

Java 1.7也解决了Kevin所描述的尴尬:读取文件现在是:

 byte[] data = Files.readAllBytes(Paths.get("source-file")); 

一位评论者问:“为什么要使用第三方库?” 答案是,自己做这件事太痛苦了。 下面是一个如何正确地执行从文件中读取字节数组的反操作的例子(对不起,这只是我已经可用的代码,并不像我希望提交者实际粘贴和使用此代码):

 public static byte[] toByteArray(File file) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean threw = true; InputStream in = new FileInputStream(file); try { byte[] buf = new byte[BUF_SIZE]; long total = 0; while (true) { int r = in.read(buf); if (r == -1) { break; } out.write(buf, 0, r); } threw = false; } finally { try { in.close(); } catch (IOException e) { if (threw) { log.warn("IOException thrown while closing", e); } else { throw e; } } } return out.toByteArray(); } 

每个人都应该为此感到震惊。

使用好的库。 毫不奇怪,我推荐Guava的Files.write(byte [],File) 。

要将字节数组写入文件,请使用该方法

 public void write(byte[] b) throws IOException 

来自BufferedOutputStream类。

java.io.BufferedOutputStream实现了一个缓冲输出stream。 通过设置这样的输出stream,应用程序可以将字节写入底层输出stream,而不必为写入的每个字节调用底层系统。

对于你的例子你需要像这样的东西:

 String filename= "C:/SO/SOBufferedOutputStreamAnswer"; BufferedOutputStream bos = null; try { //create an object of FileOutputStream FileOutputStream fos = new FileOutputStream(new File(filename)); //create an object of BufferedOutputStream bos = new BufferedOutputStream(fos); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = kgen.generateKey(); byte[] encoded = key.getEncoded(); bos.write(encoded); } // catch and handle exceptions... 

Apache Commons IO Utils有一个FileUtils.writeByteArrayToFile()方法。 请注意,如果您正在执行任何文件/ IO工作,那么Apache Commons IO库将为您执行大量工作。

不需要外部库来扩大事情 – 特别是使用Android时。 这是一个原生的解决scheme,这个技巧。 这是一个从一个应用程序的代码pice存储一个字节数组作为一个图像文件。

  // Byte array with image data. final byte[] imageData = params[0]; // Write bytes to tmp file. final File tmpImageFile = new File(ApplicationContext.getInstance().getCacheDir(), "scan.jpg"); FileOutputStream tmpOutputStream = null; try { tmpOutputStream = new FileOutputStream(tmpImageFile); tmpOutputStream.write(imageData); Log.d(TAG, "File successfully written to tmp file"); } catch (FileNotFoundException e) { Log.e(TAG, "FileNotFoundException: " + e); return null; } catch (IOException e) { Log.e(TAG, "IOException: " + e); return null; } finally { if(tmpOutputStream != null) try { tmpOutputStream.close(); } catch (IOException e) { Log.e(TAG, "IOException: " + e); } }