How to convert StringReader to String?

I am trying to convert my StringReaderback to normal Stringas shown:

String string = reader.toString();

But when I try to read this line like this:

System.out.println("string: "+string);

All I get is a pointer value, for example:

java.io.StringReader@2c552c55

Am I doing something wrong while reading a line back?

+13
source share
8 answers

The method StringReader toStringdoes not return internal buffers StringReader.

You will need to read out StringReaderto get this.

I recommend using read overload, which takes an array of characters. Mass reads are faster than unambiguous.

t

//use string builder to avoid unnecessary string creation.
StringBuilder builder = new StringBuilder();
int charsRead = -1;
char[] chars = new char[100];
do{
    charsRead = reader.read(chars,0,chars.length);
    //if we have valid chars, append them to end of string.
    if(charsRead>0)
        builder.append(chars,0,charsRead);
}while(charsRead>0);
String stringReadFromReader = builder.toString();
System.out.println("String read = "+stringReadFromReader);
+10
source
import org.apache.commons.io.IOUtils;

String string = IOUtils.toString(reader);
+21
source

CharStreams Googles. Guava:

CharStreams.toString(stringReader);
+4

:

 Scanner scanner = new Scanner(reader).useDelimiter("\\A");
 String str = scanner.hasNext() ? scanner.next() : "";

hasNext() , next() NoSuchElementException, () .

+3

reader.toString(); toString() Object.

read():

int i;               
do {
    i = reader.read();
    char c = (char) i;
    // do whatever you want with the char here...

} while (i != -1);   
+1

toString() StringReader. , StringReader :

public class StringReaderExample {

   public static void main(String[] args) {

      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) sr.read();
            System.out.print("" + c);
         }

         // close the stream
         sr.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

.

+1

toString() StringReader, String, StringReader.

You need to use methods read()and / or read(char[] cbuf, int off, int len)to read the actual characters in the string.

0
source

If you use a method toString()in an object StringReader, you will print the memory position of the object. You use one of the following methods:

read() Reads one character.

read(char[] cbuf, int off, int len) Reads characters to part of an array.

Here is an example:

     String s = "Hello World";

    // create a new StringReader
    StringReader sr = new StringReader(s);

    try {
        // read the first five chars
        for (int i = 0; i < s.length(); i++) {
            char c = (char) sr.read();
            System.out.print("" + c);
        }

        // close the stream
        sr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
0
source

Source: https://habr.com/ru/post/1619940/


All Articles