ด้วย Java:
ฉันมีbyte[]ที่แสดงถึงไฟล์
ฉันจะเขียนสิ่งนี้ลงไฟล์ได้อย่างไร (เช่น C:\myfile.pdf )
ฉันรู้ว่ามันเสร็จสิ้นแล้วด้วย InputStream แต่ฉันไม่สามารถใช้งานได้
ด้วย 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 {} finally {}เพื่อให้แน่ใจว่าการล้างทรัพยากรที่เหมาะสม
                    ไม่มีห้องสมุด:
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 ในบางจุดเช่นกัน
นอกจากนี้ตั้งแต่ 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
ฉันรู้ว่ามันทำกับ InputStream
ที่จริงแล้วคุณต้องการจะเขียนไปยังแฟ้มผลลัพธ์ ...
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] ไฟล์ไปยังไบต์ [] /////////////////// 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-oriented-input-output-in-cactoos.html