J’ai écrit le code suivant:
public class WriteToCharBuffer { public static void main(Ssortingng[] args) { Ssortingng text = "This is the data to write in buffer!\nThis is the second line\nThis is the third line"; OutputStream buffer = writeToCharBuffer(text); readFromCharBuffer(buffer); } public static OutputStream writeToCharBuffer(Ssortingng dataToWrite){ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(byteArrayOutputStream)); try { bufferedWriter.write(dataToWrite); bufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); } return byteArrayOutputStream; } public static void readFromCharBuffer(OutputStream buffer){ ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) buffer; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))); Ssortingng line = null; SsortingngBuffer sb = new SsortingngBuffer(); try { while ((line = bufferedReader.readLine()) != null) { //System.out.println(line); sb.append(line); } System.out.println(sb); } catch (IOException e) { e.printStackTrace(); } } }
Lorsque j’exécute le code ci-dessus, voici le résultat:
This is the data to write in buffer!This is the second lineThis is the third line
Pourquoi les caractères de nouvelle ligne (\ n) sont-ils ignorés? Si je ne commente pas le System.out.println () comme suit:
while ((line = bufferedReader.readLine()) != null) { System.out.println(line); sb.append(line); }
Je reçois la sortie correcte en tant que:
This is the data to write in buffer! This is the second line This is the third line This is the data to write in buffer!This is the second lineThis is the third line
Quelle est la raison pour cela?
Dit JavaDoc
public Ssortingng readLine() throws IOException
Lit une ligne de texte. Une ligne est considérée comme terminée par un saut de ligne (‘\ n’), un retour chariot (‘\ r’) ou un retour chariot suivi immédiatement d’un saut de ligne.
Résultats:
Une chaîne contenant le contenu de la ligne, n’incluant aucun caractère de fin de ligne, ou null si la fin du stream est atteinte
Jette:
De Javadoc
Lire une ligne de texte. Une ligne est considérée comme terminée par un saut de ligne (‘\ n’) , un retour chariot (‘\ r’) ou un retour chariot suivi immédiatement par un saut de ligne.
Tu peux faire quelque chose comme ça
buffer.append(line); buffer.append(System.getProperty("line.separator"));
Juste au cas où quelqu’un voudrait lire le texte avec '\n'
inclus.
essayez cette approche simple
Alors,
Dis, tu as trois lignes de données (disons dans un fichier .txt
), comme ceci
This is the data to write in buffer! This is the second line This is the third line
Et en lisant , vous faites quelque chose comme ça
Ssortingng content=null; Ssortingng str=null; while((str=bufferedReader.readLine())!=null){ //assuming you have content.append(str); //your bufferedReader declared. } bufferedReader.close(); System.out.println(content);
et s’attendant à ce que la sortie soit
This is the data to write in buffer! This is the second line This is the third line
mais en se grattant la tête en voyant la sortie comme une seule ligne
This is the data to write in buffer!This is the second lineThis is the third line
Voici ce que vous pouvez faire
en ajoutant ce morceau de code à l’intérieur de votre boucle while
if(str.sortingm().length()==0){ content.append("\n"); }
Alors maintenant, à quoi devrait ressembler votre boucle while
while((str=bufferedReader.readLine())!=null){ if(str.sortingm().length()==0){ content.append("\n"); } content.append(str); }
Maintenant, vous obtenez la sortie requirejse (en trois lignes de texte)
This is the data to write in buffer! This is the second line This is the third line
C’est ce que disent les javadocs pour la méthode readLine () de la classe BufferedReader.
/** * Reads a line of text. A line is considered to be terminated by any one * of a line feed ('\n'), a carriage return ('\r'), or a carriage return * followed immediately by a linefeed. * * @return A Ssortingng containing the contents of the line, not including * any line-termination characters, or null if the end of the * stream has been reached * * @exception IOException If an I/O error occurs */
readline()
ne retourne pas la fin de la ligne des plateformes. JavaDoc .
C’est à cause de readLine (). De Java Docs :
Lire une ligne de texte. Une ligne est considérée comme terminée par un saut de ligne (‘\ n’), un retour chariot (‘\ r’) ou un retour chariot suivi immédiatement d’un saut de ligne.
Donc, ce qui se passe, c’est que vos “\ n” sont considérés comme un saut de ligne, le lecteur considère donc qu’il s’agit d’une ligne.