2010-05-13 3 views

답변

12

이미 아파치 commons-io를 사용하는 경우, 당신은 그것을 할 수 있습니다

IOUtils.copy(byteArrayInputStream, new FileOutputStream(outputFileName)); 
+0

예. 훨씬 더 청결한. – SingleShot

+0

이것은 훌륭하지만, 나는 그것을 닫을 수 있도록 복사 호출 외의 FileoutputStream을 생성해야한다는 것을 알았다. IOUtils 중 일부는 버퍼를 플러시하지만 출력 파일이 열리지 않는 문제가있었습니다. FileOutputStream에서 close()에 대한 호출을 추가하면 훌륭하게 작동했습니다. 전반적으로, 나는 다행스럽게도 IOUtils 항목을 발견했으며 다른 것들에도 사용하고 있습니다. – titania424

2

다음과 같은 코드를 사용할 수 있습니다

ByteArrayInputStream input = getInputStream(); 
FileOutputStream output = new FileOutputStream(outputFilename); 

int DEFAULT_BUFFER_SIZE = 1024; 
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 
long count = 0; 
int n = 0; 

n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE); 

while (n >= 0) { 
    output.write(buffer, 0, n); 
    n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE); 
} 
+0

감사합니다 Gaurav, 지금 시도 할 것입니다. – Ankur

5
InputStream in = //your ByteArrayInputStream here 
OutputStream out = new FileOutputStream("filename.jpg"); 

// Transfer bytes from in to out 
byte[] buf = new byte[1024]; 
int len; 
while ((len = in.read(buf)) > 0) { 
    out.write(buf, 0, len); 
} 
in.close(); 
out.close(); 
-3
ByteArrayInputStream stream = <<Assign stream>>; 
    byte[] bytes = new byte[1024]; 
    stream.read(bytes); 
    BufferedWriter writer = new BufferedWriter(new FileWriter(new File("FileLocation"))); 
    writer.write(new String(bytes)); 
    writer.close(); 

버퍼 작가가 성능이 향상됩니다 FileWriter와 비교하여 파일을 쓰는 중.

+1

작성자는 이진 파일이 아닌 문자 파일 용입니다. –

관련 문제