Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

WikiQuora

WikiQuora Logo WikiQuora Logo

WikiQuora Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Add group
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/string

WikiQuora Latest Questions

W3spoint99
  • 0
W3spoint99Begginer
Asked: December 31, 2024In: Java

How to read / convert an InputStream into a String in Java?

  • 0

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 ...

inputstreamioJavastreamstring
  1. Saralyn
    Saralyn Teacher
    Added an answer on December 31, 2024 at 12:08 pm

    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:

    1. Using IOUtils.toString (Apache Utils)
       String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
      
    2. Using CharStreams (Guava)
       String result = CharStreams.toString(new InputStreamReader(
             inputStream, Charsets.UTF_8));
      
    3. Using Scanner (JDK)
       Scanner s = new Scanner(inputStream).useDelimiter("\\A");
       String result = s.hasNext() ? s.next() : "";
      
    4. Using Stream API (Java 8). Warning: This solution converts different line breaks (like \r\n) to \n.
       String result = new BufferedReader(new InputStreamReader(inputStream))
         .lines().collect(Collectors.joining("\n"));
      
    5. Using parallel Stream API (Java 8). Warning: This solution converts different line breaks (like \r\n) to \n.
       String result = new BufferedReader(new InputStreamReader(inputStream))
          .lines().parallel().collect(Collectors.joining("\n"));
      
    6. Using InputStreamReader and StringBuilder (JDK)
       int bufferSize = 1024;
       char[] buffer = new char[bufferSize];
       StringBuilder out = new StringBuilder();
       Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
       for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
           out.append(buffer, 0, numRead);
       }
       return out.toString();
      
    7. Using StringWriter and IOUtils.copy (Apache Commons)
       StringWriter writer = new StringWriter();
       IOUtils.copy(inputStream, writer, "UTF-8");
       return writer.toString();
      
    8. Using ByteArrayOutputStream and inputStream.read (JDK)
       ByteArrayOutputStream result = new ByteArrayOutputStream();
       byte[] buffer = new byte[1024];
       for (int length; (length = inputStream.read(buffer)) != -1; ) {
           result.write(buffer, 0, length);
       }
       // StandardCharsets.UTF_8.name() > JDK 7
       return result.toString("UTF-8");
      
    9. Using BufferedReader (JDK). Warning: This solution converts different line breaks (like \n\r) to line.separator system property (for example, in Windows to “\r\n”).
       String newLine = System.getProperty("line.separator");
       BufferedReader reader = new BufferedReader(
               new InputStreamReader(inputStream));
       StringBuilder result = new StringBuilder();
       for (String line; (line = reader.readLine()) != null; ) {
           if (result.length() > 0) {
               result.append(newLine);
           }
           result.append(line);
       }
       return result.toString();
      
    10. Using BufferedInputStream and ByteArrayOutputStream (JDK)
      BufferedInputStream bis = new BufferedInputStream(inputStream);
      ByteArrayOutputStream buf = new ByteArrayOutputStream();
      for (int result = bis.read(); result != -1; result = bis.read()) {
          buf.write((byte) result);
      }
      // StandardCharsets.UTF_8.name() > JDK 7
      return buf.toString("UTF-8");
      
    11. Using inputStream.read() and StringBuilder (JDK). Warning: This solution has problems with Unicode, for example with Russian text (works correctly only with non-Unicode text)
      StringBuilder sb = new StringBuilder();
      for (int ch; (ch = inputStream.read()) != -1; ) {
          sb.append((char) ch);
      }
      return sb.toString();
      

    Warning:

    1. Solutions 4, 5 and 9 convert different line breaks to one.
    2. Solution 11 can’t work correctly with Unicode text

    Performance tests

    Performance tests for small String (length = 175), url in github (mode = Average Time, system = Linux, score 1,343 is the best):

                  Benchmark                         Mode  Cnt   Score   Error  Units
     8. ByteArrayOutputStream and read (JDK)        avgt   10   1,343 ± 0,028  us/op
     6. InputStreamReader and StringBuilder (JDK)   avgt   10   6,980 ± 0,404  us/op
    10. BufferedInputStream, ByteArrayOutputStream  avgt   10   7,437 ± 0,735  us/op
    11. InputStream.read() and StringBuilder (JDK)  avgt   10   8,977 ± 0,328  us/op
     7. StringWriter and IOUtils.copy (Apache)      avgt   10  10,613 ± 0,599  us/op
     1. IOUtils.toString (Apache Utils)             avgt   10  10,605 ± 0,527  us/op
     3. Scanner (JDK)                               avgt   10  12,083 ± 0,293  us/op
     2. CharStreams (guava)                         avgt   10  12,999 ± 0,514  us/op
     4. Stream Api (Java 8)                         avgt   10  15,811 ± 0,605  us/op
     9. BufferedReader (JDK)                        avgt   10  16,038 ± 0,711  us/op
     5. parallel Stream Api (Java 8)                avgt   10  21,544 ± 0,583  us/op
    

    Performance tests for big String (length = 50100), url in github (mode = Average Time, system = Linux, score 200,715 is the best):

                   Benchmark                        Mode  Cnt   Score        Error  Units
     8. ByteArrayOutputStream and read (JDK)        avgt   10   200,715 ±   18,103  us/op
     1. IOUtils.toString (Apache Utils)             avgt   10   300,019 ±    8,751  us/op
     6. InputStreamReader and StringBuilder (JDK)   avgt   10   347,616 ±  130,348  us/op
     7. StringWriter and IOUtils.copy (Apache)      avgt   10   352,791 ±  105,337  us/op
     2. CharStreams (guava)                         avgt   10   420,137 ±   59,877  us/op
     9. BufferedReader (JDK)                        avgt   10   632,028 ±   17,002  us/op
     5. parallel Stream Api (Java 8)                avgt   10   662,999 ±   46,199  us/op
     4. Stream Api (Java 8)                         avgt   10   701,269 ±   82,296  us/op
    10. BufferedInputStream, ByteArrayOutputStream  avgt   10   740,837 ±    5,613  us/op
     3. Scanner (JDK)                               avgt   10   751,417 ±   62,026  us/op
    11. InputStream.read() and StringBuilder (JDK)  avgt   10  2919,350 ± 1101,942  us/op
    

    Graphs (performance tests depending on Input Stream length in Windows 7 system)
    enter image description here

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

     length  182    546     1092    3276    9828    29484   58968
    
     test8  0.38    0.938   1.868   4.448   13.412  36.459  72.708
     test4  2.362   3.609   5.573   12.769  40.74   81.415  159.864
     test5  3.881   5.075   6.904   14.123  50.258  129.937 166.162
     test9  2.237   3.493   5.422   11.977  45.98   89.336  177.39
     test6  1.261   2.12    4.38    10.698  31.821  86.106  186.636
     test7  1.601   2.391   3.646   8.367   38.196  110.221 211.016
     test1  1.529   2.381   3.527   8.411   40.551  105.16  212.573
     test3  3.035   3.934   8.606   20.858  61.571  118.744 235.428
     test2  3.136   6.238   10.508  33.48   43.532  118.044 239.481
     test10 1.593   4.736   7.527   20.557  59.856  162.907 323.147
     test11 3.913   11.506  23.26   68.644  207.591 600.444 1211.545
    See less
      • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
1
  • 1 1 Answer
  • 286 Views
Answer
W3spoint99
  • 0
W3spoint99Begginer
Asked: December 30, 2024In: Linux

Find all files containing a specific text (string) on Linux?

  • 0

How do I find all files containing a specific string of text within their file contents? The following doesn’t work. It seems to display every single file in the system. find / -type f -exec grep -H 'text-to-find-here' {} \;

directoryfilesfindgreplinuxstring
  1. Saralyn
    Saralyn Teacher
    Added an answer on December 30, 2024 at 7:39 am

    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'
    
    • -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, --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-dir parameter. For example, this will exclude the dirs dir1/, 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
      • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
1
  • 1 1 Answer
  • 284 Views
Answer

Sidebar

Ask A Question
  • Popular
  • Answers
  • W3spoint99

    What is the difference between Promises and Observables?

    • 2 Answers
  • W3spoint99

    Can't bind to 'ngModel' since it isn't a known property ...

    • 2 Answers
  • W3spoint99

    How to prevent SQL injection in PHP?

    • 1 Answer
  • Saralyn
    Saralyn added an answer Learn Java if: ✅ You want to work on enterprise applications.… April 27, 2025 at 2:01 pm
  • Saralyn
    Saralyn added an answer AI is getting smarter, but replacing programmers entirely? That’s not… April 27, 2025 at 1:58 pm
  • Saralyn
    Saralyn added an answer Both Promises and Observables provide us with abstractions that help us deal with the asynchronous nature… January 17, 2025 at 2:03 pm

Trending Tags

AI angular application.properties arrays artificial intelligence coding how Java javascript machine learning mysql nullpointerexception php programmer python reactjs spring springboot sql string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

  • About US
  • Privacy Policy
  • Questions
  • Recent Questions
  • Web Stories

© 2025 WikiQuora.Com. All Rights Reserved