2016-04-04 80 views
-1

İki uygulama arasında ikili veriyi iletmek için FileOutputStream - Byte dizisini dönüştürmek istiyorum. lütfen herhangi biri yardımcı olabilir?mümkün mü?

+0

gibi ByteArrayOutputStream kullanabilir iyi soru sormak için nasıl bu sayfayı okuyun. Ne denedin? Araştırmanı göster. Neden yapmaya çalıştığını çalışmıyor? – 0xDEADC0DE

cevap

2

Bir dosyayı bayt dizisine dönüştürmek için ByteArrayOutputStream sınıfı kullanılır. Bu sınıf, verilerin bir bayt dizisine yazıldığı bir çıktı akışını uygular. Veri otomatik olarak yazıldığında tampon otomatik olarak büyür. Veriler toByteArray() ve toString() kullanılarak alınabilir.

Bayt dizisini tekrar orijinal dosyaya dönüştürmek için FileOutputStream sınıfı kullanılır. Bir dosya çıktı akışı, bir Dosyaya veya bir FileDescriptor'a veri yazmak için bir çıkış akımıdır.

Aşağıdaki kod tamamen sınanmıştır. Bir FileOutputStream kullanarak bir dosyaya bayt dizisi nasıl yazılır

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

FileOutputStream, bir dosyaya veya bir FileDescriptor'a veri yazmak için bir çıktı akıştır. stackoverflow.com/help/how-to-ask:

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

     } 

    } 
0

Bunu

private byte[] filetoByteArray(String path) { 
    byte[] data; 
    try { 
     InputStream input = new FileInputStream(path); 
     int byteReads; 
     ByteArrayOutputStream output = new ByteArrayOutputStream(1024); 
     while ((byteReads = inputStream.read()) != -1) { 
      output.write(byteReads); 
     } 

     data = output.toByteArray(); 
     output.close(); 
     input.close(); 
     return data; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
}