2011-09-18 2 views
0

파일을 받으려고합니다. inputstream을 얻어 문자열로 변환하여 내 메서드에 전달합니다. 거기에서 바이트 배열로 변환 한 다음 다시 파일로 변환하려고합니다. 나는이 결과를 얻을 수 없다. 이것은 파일에 문자열을 쓰는 방법입니다.자바 입력 스트림을 파일로 다시 문자열

String result = convertStreamToString(is); 
byte[] b = result.getBytes(); 
FileOutputStream fos = null;   
fos = new FileOutputStream("/sdcard/chats/ReceivedFile"); 

Log.i("IMSERVICE", "FILERECCC-1");  
if (b!= null) 
{ 
    result = convertStreamToString(is);    
    result = result.replace("\n", ""); 
    Log.e("InputStream output","FILEREC"); 

    //IOUtils.copy(is,fos); 

    byte[] buffer = new byte[1024]; 
    int length;  
    while ((length = b.length) > 0) 
    {  
     fos.write(buffer, 0, length); 
    } 

아무도 도와 줄 수 있습니까?

+0

나는 당신의 while 루프가 문제가 내 코드를 시도 만들 생각합니다. – user370305

답변

0

왜 열심히해야합니까? 파일의 writting에 입력 스트림의

그냥 직접 사용,이 같은

,

try { 

    // Open your input stream 
    InputStream myInput; //this is your input stream 

    // Path to the your output file 
    String outFileName = "your output file name"; 

    OutputStream myOutput = new FileOutputStream(outFileName); 

    // transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 

    while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
      } 

     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

     } catch (Exception e) { 

      Log.e("error", e.toString()); 

     } 
+0

잘 그 첫 번째 수신에 있기 때문에 내가 버퍼링 된 판독기를 사용하여 문자 배열로 파일을 변환, 즉 들어오는 스트림이 텍스트 메시지 또는 파일 경우) 다음 몇 가지 정보를 확인하는 것입니다 그 다음 파일을 전달하면 메서드는 아니지만 메서드는 버퍼링 된 판독기의 while 루프 내에서 호출됩니다. 그래서 내가 원시 스트림을 파일에 쓰지 않을 때 어떤 이유로 든. 왜 내가 이러한 변환을 수행해야합니다. 고맙습니다. – user951019

관련 문제