有可能将FileOutputStream转换为字节数组? [英] is possible to convert FileOutputStream to byte array?

查看:219
本文介绍了有可能将FileOutputStream转换为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将FileOutputStream转换为Byte数组,以在两个应用程序之间传递二进制数据。请任何人都可以帮忙?

解决方案

要将文件转换为字节数组,需要使用ByteArrayOutputStream类。这个类实现了将数据写入字节数组的输出流。数据写入时,缓冲区会自动增长。可以使用toByteArray()和toString()来检索数据。

要将字节数组转换回原始文件,使用FileOutputStream类。文件输出流是将数据写入文件或文件描述符的输出流。

以下代码已经过全面测试。

  public static void main(String [] args)抛出FileNotFoundException,IOException {
File file = new File( java.pdf);

FileInputStream fis = new FileInputStream(file);
//System.out.println(file.exists()+!!);
// InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] buf = new byte [1024];
尝试{
for(int readNum;(readNum = fis.read(buf))!= -1;){
bos.write(buf,0,readNum); //毫无疑问,这里是0
//将偏移量off的指定字节数组中的len个字节写入该字节数组输出流。
System.out.println(read+ readNum +bytes,);
}
} catch(IOException ex){
Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE,null,ex);
}
byte [] bytes = bos.toByteArray();

//下面是不同的部分
File someFile = new File(java2.pdf);
FileOutputStream fos = new FileOutputStream(someFile);
fos.write(bytes);
fos.flush();
fos.close();





$ b

如何使用FileOutputStream将字节数组写入文件。 FileOutputStream是用于将数据写入File或FileDescriptor的输出流。

  public static void main(String [] args){

String s =input text to写入输出流;

File file = new File(outputfile.txt);

FileOutputStream fos = null;

尝试{

fos = new FileOutputStream(file);

//将指定字节数组中的字节写入此文件输出流
fos.write(s.getBytes());

$ b catch(FileNotFoundException e){
System.out.println(File not found+ e);

catch(IOException ioe){
System.out.println(写入文件时出现异常+ ioe);
}
finally {
//使用close方法关闭数据流
try {
if(fos!= null){
fos.close() ;

$ b catch(IOException ioe){
System.out.println(Error stream while stream:+ ioe);
}

}

}


I want to convert FileOutputStream to Byte array for passing binary data between two applications. please any one can help?

解决方案

To convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

To convert byte array back to the original file, FileOutputStream class is used. A file output stream is an output stream for writing data to a File or to a FileDescriptor.

The following code has been fully tested.

 public static void main(String[] args) throws FileNotFoundException, IOException {
            File file = new File("java.pdf");

            FileInputStream fis = new FileInputStream(file);
            //System.out.println(file.exists() + "!!");
            //InputStream in = resource.openStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            try {
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum); //no doubt here is 0
                    //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                    System.out.println("read " + readNum + " bytes,");
                }
            } catch (IOException ex) {
                Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
            }
            byte[] bytes = bos.toByteArray();

            //below is the different part
            File someFile = new File("java2.pdf");
            FileOutputStream fos = new FileOutputStream(someFile);
            fos.write(bytes);
            fos.flush();
            fos.close();
        }

how to write a byte array to a file using a FileOutputStream. The FileOutputStream is an output stream for writing data to a File or to a FileDescriptor.

public static void main(String[] args) {

        String s = "input text to be written in output stream";

        File file = new File("outputfile.txt");

        FileOutputStream fos = null;

        try {

            fos = new FileOutputStream(file);

            // Writes bytes from the specified byte array to this file output stream 
            fos.write(s.getBytes());

        }
        catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        }
        catch (IOException ioe) {
            System.out.println("Exception while writing file " + ioe);
        }
        finally {
            // close the streams using close method
            try {
                if (fos != null) {
                    fos.close();
                }
            }
            catch (IOException ioe) {
                System.out.println("Error while closing stream: " + ioe);
            }

        }

    }

这篇关于有可能将FileOutputStream转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆