If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is ...
Do the following: grep -rnw '/path/to/somewhere/' -e 'pattern' -r or -R is recursive, -n is line number, and -w stands for match the whole word. -l (lower-case L) can be added to just give the file name of matching files. -e is the pattern used during the search Along with these, --exclude, --includRead more
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-ror-Ris recursive,-nis line number, and-wstands for match the whole word.-l(lower-case L) can be added to just give the file name of matching files.-eis the pattern used during the search
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" - This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern" - For directories it’s possible to exclude one or more directories using the
--exclude-dirparameter. For example, this will exclude the dirsdir1/,dir2/and all of them matching*.dst/:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
See less
To summarize the other answers, I found 11 main ways to do this (see below). And I wrote some performance tests (see results below): Ways to convert an InputStream to a String: Using IOUtils.toString (Apache Utils) String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); Using CharStreRead more
To summarize the other answers, I found 11 main ways to do this (see below). And I wrote some performance tests (see results below):
Ways to convert an InputStream to a String:
IOUtils.toString(Apache Utils)CharStreams(Guava)Scanner(JDK)\r\n) to\n.\r\n) to\n.InputStreamReaderandStringBuilder(JDK)StringWriterandIOUtils.copy(Apache Commons)ByteArrayOutputStreamandinputStream.read(JDK)BufferedReader(JDK). Warning: This solution converts different line breaks (like\n\r) toline.separatorsystem property (for example, in Windows to “\r\n”).BufferedInputStreamandByteArrayOutputStream(JDK)inputStream.read()andStringBuilder(JDK). Warning: This solution has problems with Unicode, for example with Russian text (works correctly only with non-Unicode text)Warning:
Performance tests
Performance tests for small
String(length = 175), url in github (mode = Average Time, system = Linux, score 1,343 is the best):Performance tests for big
String(length = 50100), url in github (mode = Average Time, system = Linux, score 200,715 is the best):Graphs (performance tests depending on Input Stream length in Windows 7 system)

Performance test (Average Time) depending on Input Stream length in Windows 7 system:
See less