如何在Java中向文件写入字节数组?
如何用Java将字节数组写入文件?现在最直接的一点就是。有关详细信息,请参阅如何在Java中向文件写入字节数组?,java,Java,如何用Java将字节数组写入文件?现在最直接的一点就是。有关详细信息,请参阅 旧答案: 这是最直截了当的。您要写入的数据是什么 这可能对您有所帮助。有一个方法。请注意,如果您正在做任何文件/IO工作,那么Apache Commons IO库将为您做大量工作。您可以从中使用 要将字节数组写入文件,请使用以下方法 public void write(byte[] b) throws IOException 来自BufferedOutputStream类 实现缓冲输出流。通过设置这样的输出流,应用
旧答案: 这是最直截了当的。您要写入的数据是什么 这可能对您有所帮助。有一个方法。请注意,如果您正在做任何文件/IO工作,那么Apache Commons IO库将为您做大量工作。您可以从中使用
要将字节数组写入文件,请使用以下方法
public void write(byte[] b) throws IOException
来自BufferedOutputStream类
实现缓冲输出流。通过设置这样的输出流,应用程序可以将字节写入底层输出流,而不必为写入的每个字节调用底层系统
对于您的示例,您需要以下内容:
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...
一位评论者问“为什么要使用第三方库来做这个?”答案是,自己做太痛苦了。下面是一个如何正确执行从文件读取字节数组的反向操作的示例(抱歉,这只是我现成的代码,我并不希望询问者实际粘贴并使用此代码):
每个人都应该对这种痛苦感到震惊
使用好的库。毫不奇怪,我推荐番石榴。从Java 1.7开始,有一种新方法: Java 1.7还解决了Kevin描述的尴尬:现在读取文件就是:
byte[] data = Files.readAllBytes(Paths.get("source-file"));
不需要外部LIB来扩充内容-尤其是在使用Android时。这里有一个本机解决方案。这是一段应用程序的代码,该应用程序将字节数组存储为图像文件
// 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);
}
}
但如果字节数组长度超过1024,则应使用循环写入数据。byte[]encoded=key.getEncoded();我需要将编码写入文本文件KeyGenerator kgen=KeyGenerator.getInstance(“AES”);kgen.init(128);SecretKey=kgen.generateKey();byte[]encoded=key.getEncoded();然后简单地使用FileOutputStream.+1来提及教程。。。(如果你提到www.google.com,你甚至会收到一个+2-好吧,这很糟糕,但这不是rovers的第一个问题…)你应该按照@JuanZe的建议将FileOutputStream包装在BufferedOutputStream中。性能要好得多。+1表示缓冲输出流。您应该始终将FileOutputStream包装在BufferedOutputStream中,这样性能会更好。如果您只想写入16字节的密钥,则将FileOutputStream包装在BufferedOutputStream中可能比将数据直接写入FileOutputStream慢。@SamBarnum您能详细说明一下吗?为什么在BOS中封装FOS会更快?这取决于您使用的write()方法。如果一次写入一个字节(或以小块的形式),则会导致大量磁盘活动。如果您的磁盘安装在网络上,这可能是灾难性的。他确实说过“可能是”灾难性的。老兄,该死的开发人员总是要挑剔和争论PWhy为此添加第三方库?OP没有询问byteArray的AES编码。@GauravAgarwal没有在她/他的原始问题中(他们应该已经更新了!),但请参见此处的第一条和第三条评论:@lutz正在为Apache Commons做PR。FileOutputStream输出+1=新FileOutputStream(新文件(“目标文件”);我对这是多么痛苦感到震惊,但这类东西不在标准库中。我们谈论的不是一种晦涩难懂的文件格式,而是将字节从内存移动到磁盘。最佳答案:Guava's Files.write(byte[],file)。这可能是目前推荐的方法。不需要所有的
SecretKey
资料;只需调用Files.write()
。谢谢你,塞巴斯蒂安!你能写一些代码并显示你到底想写什么到文件中吗?Link死了(Jim)。
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);
byte[] data = Files.readAllBytes(Paths.get("source-file"));
// 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);
}
}
File file = ...
byte[] data = ...
try{
FileOutputStream fos = FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
}catch(Exception e){
}