In this post under Apache Commons IO, I will show with example how to write String data to an output stream.
The Apache Commons IO library, provides a overloaded static method “write” under “IOUtils” class.
We can use this method to write String data to an output stream.
Its just a one line of code.
Main class
package ioutils;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Example10 {
public static void main(String[] args) throws IOException {
String input = "No man is a true believer unless he desires for his brother that which he desires for himself. -Prophet\n" +
"\n" +
"Limits are what separates us from animals. -Satyameva Jayate\n" +
"\n" +
"For a new start, it is necessary to end what is going on - Murder 3\n" +
"\n" +
"A person gets a reply from the place where he is scared to go - Murder 3\n" +
"\n" +
"The strong man who has known power all his life, may lose respect for that power, but a weak man knows the value of strength, and knows compassion. - Captain America The first Avenger\n" +
"\n" +
"A person is a fool if he knows just the truth and not the difference between the truth and the lie - Badla";
IOUtils.write(input, System.out, StandardCharsets.UTF_8);
}
}
In the above code, I created String data named “input”.
Now I write the “input” data to console output by calling “write” and passing the three arguments
1) input
2) instance of OutputStream
3) Charset to choose.
In this way we can write String data to output stream in just one line.