"Ciao, Amico!"

"Ciao, Eli!"

"Oggi voglio parlarti delle classi StringReader e StringWriter . In linea di principio, non ci sarà molto di nuovo per te, ma a volte queste classi sono molto utili. Ma, almeno, voglio che tu sappia che esistano».

"Queste classi sono le implementazioni più semplici delle classi astratte Reader e Writer. E sono fondamentalmente simili a FileReader e FileWriter. Tuttavia, a differenza di queste, queste non funzionano con i dati in un file su disco. Funzionano invece con una stringa nella memoria della JVM."

"Perché avremmo bisogno di tali classi?"

"A volte sono necessari. Essenzialmente, StringReader è un adattatore tra le classi String e Reader . E StringWriter è una stringa che eredita Writer . Sì... posso dire che non è la migliore spiegazione. Sarà meglio guardare un paio prima di esempi."

"Ad esempio, supponi di voler testare il tuo metodo, che legge i dati dall'oggetto Reader che gli è stato passato. Potremmo farlo in questo modo:"

Lettura da un oggetto Reader:
public static void main (String[] args) throws Exception
{
 String test = "Hi!\n My name is Richard\n I'm a photographer\n";

 // This line is key: we "convert" the String into a Reader.
 StringReader reader = new StringReader(test);

 executor(reader);
}

public static void executor(Reader reader) throws Exception
{
 BufferedReader br = new BufferedReader(reader);
 String line;
 while (line = br.readLine() != null)
 {
 System.out.println(line);
 }
}

"In altre parole, abbiamo semplicemente preso una stringa, l'abbiamo racchiusa in uno StringReader e poi l'abbiamo passata al posto di un oggetto Reader? E tutto verrà letto da esso come abbiamo bisogno?"

"Sì. Hmm. E c'è un punto in questo. Ora testiamo i metodi di StringWriter. Per fare ciò, renderemo l'esempio più complicato. Ora non leggerà semplicemente le righe e le visualizzerà sullo schermo, invece li invertirà e li restituirà all'oggetto scrittore. Ad esempio:"

Leggere dall'oggetto lettore e scrivere sull'oggetto scrittore:
public static void main (String[] args) throws Exception
{
 // The Reader must read this String.
 String test = "Hi!\n My name is Richard\n I'm a photographer\n";

 // Wrap the String in a StringReader.
 StringReader reader = new StringReader(test);

 // Create a StringWriter object.
 StringWriter writer = new StringWriter();

 // Copy strings from the Reader to the Writer, after first reversing them.
 executor(reader, writer);

 // Get the text that was written to the Writer.
 String result = writer.toString();

 // Display the text from the Writer on the screen.
 System.out.println("Result: "+ result);
}

public static void executor(Reader reader, Writer writer) throws Exception
{
 BufferedReader br = new BufferedReader(reader);
String line;

 // Read a string from the Reader.
while (line = br.readLine()) != null)
 {

 // Reverse the string.
  StringBuilder sb = new StringBuilder(line);
  String newLine = sb.reverse().toString();

 // Write the string to the Writer.
  writer.write(newLine);
 }
}

"Abbiamo creato un oggetto StringWriter che contiene una stringa che memorizza tutto ciò che è stato scritto su questo writer . E per ottenerlo, devi solo chiamare il metodo toString ()."

"Hmm. In qualche modo sembra tutto troppo facile. Il metodo executor funziona con gli oggetti stream reader e writer , ma stiamo lavorando con le stringhe nel metodo main.

"È davvero tutto così semplice?"

"Sì. Per convertire una stringa in un Reader , basta scrivere questo:"

Creazione di un lettore da una stringa
String s = "data";
Reader reader = new StringReader(s);

"E convertire uno StringWriter in una stringa è ancora più semplice:"

Ottenere una stringa da uno scrittore
Writer writer = new StringWriter();
/* Here we write a bunch of data to the writer */
String result = writer.toString();

"Queste sono lezioni eccellenti, secondo me. Grazie per avermele raccontate, Ellie."