与Java:

我有一个字节[],代表一个文件。

我如何写这个文件(即。C: \ myfile.pdf)

我知道它是用InputStream完成的,但我似乎无法解决它。


当前回答

同样从Java 7开始,一行Java .nio.file. files:

Files.write(new File(filePath).toPath(), data);

其中data是字节[],filePath是字符串。您还可以使用StandardOpenOptions类添加多个文件打开选项。添加投掷或环绕尝试/接。

其他回答

这是一个程序,我们正在读取和打印数组的字节偏移量和长度使用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

////////////////////////// 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] Byte[]文件 ///////////////////////////

 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);
                            }

使用Apache Commons IO

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
}

你可以试试仙人掌:

new LengthOf(new TeeInput(array, new File("a.txt"))).value();

详情:http://www.yegor256.com/2017/06/22/object-oriented-input-output-in-cactoos.html

尝试OutputStream或者更具体地说FileOutputStream