使用 Java:
我有一个代表文件的 byte[]
。
如何将其写入文件(即 C:\myfile.pdf
)
我知道它是用 InputStream 完成的,但我似乎无法解决它。
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 (FileOutputStream stream = new FileOutputStream(path)) {
stream.write(bytes);
}
使用 Google Guava:
Files.write(bytes, new File(path));
使用 Apache Commons:
FileUtils.writeByteArrayToFile(new File(path), bytes);
所有这些策略都要求您在某个时候也捕获 IOException。
path
的@pavan 字符集? FileOutputStream 文档没有提到这一点,因此这可能是特定于平台的。我猜在大多数情况下它是 UTF-8。 bytes
按原样编写,不涉及字符集。
使用 java.nio.file
的另一种解决方案:
byte[] bytes = ...;
Path path = Paths.get("C:\\myfile.pdf");
Files.write(path, bytes);
C:\myfile.pdf
无论如何都不能在 Android 上运行... ;)
同样从 Java 7 开始,一行 java.nio.file.Files:
Files.write(new File(filePath).toPath(), data);
其中 data 是你的 byte[] 并且 filePath 是一个字符串。您还可以使用 StandardOpenOptions 类添加多个文件打开选项。使用 try/catch 添加 throws 或环绕。
Paths.get(filePath);
而不是 new File(filePath).toPath()
从 Java 7 起,您可以使用 try-with-resources 语句来避免资源泄漏并使代码更易于阅读。更多关于该here。
要将您的 byteArray
写入文件,您可以:
try (FileOutputStream fos = new FileOutputStream("fullPathToFile")) {
fos.write(byteArray);
} catch (IOException ioe) {
ioe.printStackTrace();
}
尝试使用 OutputStream
或更具体的 FileOutputStream
基本示例:
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) {}
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 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-oriented-input-output-in-cactoos.html
不定期副业成功案例分享
try {} finally {}
来确保正确的资源清理。