Answers:
FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)
或者,如果您坚持要自己做...
try (FileOutputStream fos = new FileOutputStream("pathname")) {
fos.write(myByteArray);
//fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically close the OutputStream
}
try {} finally {}
以确保正确清理资源。
没有任何库:
try (FileOutputStream stream = new FileOutputStream(path)) {
stream.write(bytes);
}
使用Google Guava:
Files.write(bytes, new File(path));
FileUtils.writeByteArrayToFile(new File(path), bytes);
所有这些策略都要求您在某个时刻也捕获IOException。
同样是从Java 7开始,带有java.nio.file.Files的一行:
Files.write(new File(filePath).toPath(), data);
其中data是您的byte [],filePath是一个String。您还可以使用StandardOpenOptions类添加多个文件打开选项。增加投掷或用尝试/接球包围。
Paths.get(filePath);
,而不是new File(filePath).toPath()
从Java 7开始,您可以使用try-with-resources语句来避免资源泄漏,并使代码更易于阅读。在这里更多。
要将您的内容写入byteArray
文件,您可以执行以下操作:
try (FileOutputStream fos = new FileOutputStream("fullPathToFile")) {
fos.write(byteArray);
} catch (IOException ioe) {
ioe.printStackTrace();
}
尝试一个OutputStream
或更具体的FileOutputStream
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
Path path = Paths.get(f.getAbsolutePath());
try {
Files.write(path, fileContent);
} catch (IOException ex) {
Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
}
/////////////////////////// 1]文件到字节[] ////////////////// //
Path path = Paths.get(p);
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(Agent1.class.getName()).log(Level.SEVERE, null, ex);
}
//////////////////////// 2]字节[]到文件///////////////////// ///////
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
Path path = Paths.get(f.getAbsolutePath());
try {
Files.write(path, fileContent);
} catch (IOException ex) {
Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
}
基本示例:
String fileName = "file.test";
BufferedOutputStream bs = null;
try {
FileOutputStream fs = new FileOutputStream(new File(fileName));
bs = new BufferedOutputStream(fs);
bs.write(byte_array);
bs.close();
bs = null;
} catch (Exception e) {
e.printStackTrace()
}
if (bs != null) try { bs.close(); } catch (Exception e) {}
这是一个程序,我们在其中使用String Builder读取并打印字节偏移和长度的数组,并将字节偏移长度的数组写入新文件。
` 在这里输入代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//*This is a program where we are reading and printing array of bytes offset and length using StringBuilder and Writing the array of bytes offset length to the new file*//
public class ReadandWriteAByte {
public void readandWriteBytesToFile(){
File file = new File("count.char"); //(abcdefghijk)
File bfile = new File("bytefile.txt");//(New File)
byte[] b;
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream (file);
fos = new FileOutputStream (bfile);
b = new byte [1024];
int i;
StringBuilder sb = new StringBuilder();
while ((i = fis.read(b))!=-1){
sb.append(new String(b,5,5));
fos.write(b, 2, 5);
}
System.out.println(sb.toString());
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis != null);
fis.close(); //This helps to close the stream
}catch (IOException e){
e.printStackTrace();
}
}
}
public static void main (String args[]){
ReadandWriteAByte rb = new ReadandWriteAByte();
rb.readandWriteBytesToFile();
}
}
控制台中的O / P:fghij
O / P在新文件中:cdefg
您可以尝试Cactoos:
new LengthOf(new TeeInput(array, new File("a.txt"))).value();
更多详细信息:http : //www.yegor256.com/2017/06/22/object-directional-input-output-in-cactoos.html