Efficient way to write InputStream to a File in Java 6

后端 未结 3 682
忘掉有多难
忘掉有多难 2021-02-15 02:52

I will get input stream from third party library to my application. I have to write this input stream to a file.

Following is the code snippet I tried:

p         


        
3条回答
  •  梦毁少年i
    2021-02-15 02:56

    It can get cleaner with an OutputStreamWriter:

    OutputStream outputStream = new FileOutputStream("output.txt");
    Writer writer = new OutputStreamWriter(outputStream);
    
    writer.write("data");
    
    writer.close();
    

    Instead of writing a string, you can use a Scanner on your inputStream

    Scanner sc = new Scanner(inputStream);
    while (sc.HasNext())
        //read using scanner methods
    

提交回复
热议问题