Java StringWriter (con esempi)

In questo tutorial, impareremo a conoscere Java StringWriter e le sue sottoclassi con l'aiuto di esempi.

La StringWriterclasse del java.iopacchetto può essere utilizzata per scrivere dati (in caratteri) nel buffer delle stringhe.

Estende la classe astratta Writer.

Nota : in Java, il buffer di stringa è considerato una stringa modificabile. Cioè, possiamo modificare il buffer delle stringhe. Per convertire da buffer di stringa a stringa, possiamo usare il toString()metodo.

Crea un StringWriter

Per creare un StringWriter, dobbiamo java.io.StringWriterprima importare il pacchetto. Una volta importato il pacchetto, ecco come possiamo creare lo string writer.

 // Creates a StringWriter StringWriter output = new StringWriter(); 

Qui abbiamo creato il writer di stringhe con capacità di buffer di stringhe predefinita. Tuttavia, possiamo specificare anche la capacità del buffer di stringa.

 // Creates a StringWriter with specified string buffer capacity StringWriter output = new StringWriter(int size); 

Qui, la dimensione specifica la capacità del buffer di stringa.

Metodi di StringWriter

La StringWriterclasse fornisce implementazioni per diversi metodi presenti nella Writerclasse.

metodo write ()

  • write() - scrive un singolo carattere nello scrittore di stringhe
  • write(char() array) - scrive i caratteri dall'array specificato nel writer
  • write(String data) - scrive la stringa specificata nel writer

Esempio: Java StringWriter

 import java.io.StringWriter; public class Main ( public static void main(String() args) ( String data = "This is the text in the string."; try ( // Create a StringWriter with default string buffer capacity StringWriter output = new StringWriter(); // Writes data to the string buffer output.write(data); // Prints the string writer System.out.println("Data in the StringWriter: " + output); output.close(); ) catch(Exception e) ( e.getStackTrace(); ) ) ) 

Produzione

 Dati in StringWriter: questo è il testo nella stringa. 

Nell'esempio precedente, abbiamo creato un writer di stringhe denominato output.

 StringWriter output = new StringWriter(); 

Quindi utilizziamo il write()metodo per scrivere i dati della stringa nel buffer della stringa.

Nota : abbiamo utilizzato il toString()metodo per ottenere i dati di output dal buffer di stringa in forma di stringa.

Accesso ai dati da StringBuffer

  • getBuffer() - restituisce i dati presenti nel buffer delle stringhe
  • toString() - restituisce i dati presenti nel buffer delle stringhe come una stringa

Per esempio,

 import java.io.StringWriter; public class Main ( public static void main(String() args) ( String data = "This is the original data"; try ( // Create a StringWriter with default string buffer capacity StringWriter output = new StringWriter(); // Writes data to the string buffer output.write(data); // Returns the string buffer StringBuffer stringBuffer = output.getBuffer(); System.out.println("StringBuffer: " + stringBuffer); // Returns the string buffer in string form String string = output.toString(); System.out.println("String: " + string); output.close(); ) catch(Exception e) ( e.getStackTrace(); ) ) ) 

Produzione

 StringBuffer: questo è il dato originale. String: questo è il dato originale 

Qui abbiamo utilizzato il getBuffer()metodo per ottenere i dati presenti nel buffer delle stringhe. E anche il metodo toString()restituisce i dati presenti nel buffer di stringa come una stringa.

metodo close ()

Per chiudere lo string writer, possiamo usare il close()metodo.

Tuttavia, il close()metodo non ha alcun effetto nella StringWriterclasse. Possiamo usare i metodi di questa classe anche dopo che il close()metodo è stato chiamato.

Altri metodi di StringWriter

Metodo Descrizione
flush() costringe a scrivere tutti i dati presenti nel writer nel buffer delle stringhe
append() inserisce il carattere specificato nel writer corrente

Per saperne di più, visita Java StringWriter (documentazione ufficiale di Java).

Articoli interessanti...