Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    Differenza tra CharArrayWriter e BufferedImputStream

    Sto cercando di capire le differenze tra BufferedRead e BufferedImputStream ma ho qualche dubbio:

    Mi sapreste spiegare le differenze tra questi due codici del mio libro?

    Codice PHP:
    package esempi_dal_libro;
    import java.io.*;
    public class 
    Esempio {
        public static 
    void main(String args[]) throws IOException 
            
    String s "~This is a © copyright symbol " 
              
    "but this is &copy not.\n"
            
    char buf[] = new char[s.length()]; 
            
    s.getChars(0s.length(), buf0); 
            
    CharArrayReader in = new CharArrayReader(buf); 
            
    int c
            
    boolean marked false
            try ( 
    BufferedReader f = new BufferedReader(in) ) 
            { 
              while ((
    f.read()) != -1) { 
                switch(
    c) { 
                case 
    '&'
                  if (!
    marked) { 
                    
    f.mark(32); 
                    
    marked true
                  } else { 
                    
    marked false
                  } 
                  break; 
                case 
    ';'
                  if (
    marked) { 
                    
    marked false
                    
    System.out.print("(c)"); 
                  } else 
                    
    System.out.print((charc); 
                  break; 
                case 
    ' '
                  if (
    marked) { 
                    
    marked false
                    
    f.reset(); 
                    
    System.out.print("&"); 
                  } else 
                    
    System.out.print((charc); 
                  break; 
                default: 
                  if (!
    marked
                    
    System.out.print((charc); 
                  break; 
                } 
              } 
            } catch(
    IOException e) { 
              
    System.out.println("I/O Error: " e); 
            } 
          }     


    Codice PHP:
    package esempi_dal_libro;
    import java.io.*;
    public class 
    Esempio {
        public static 
    void main(String args[]) { 
            
    String s "~This is a © copyright symbol " 
              
    "but this is &copy not.\n"
            
    byte buf[] = s.getBytes(); 
         
            
    ByteArrayInputStream in = new ByteArrayInputStream(buf); 
            
    int c
            
    boolean marked false
         
            
    // Use try-with-resources to manage the file. 
            
    try ( BufferedInputStream f = new BufferedInputStream(in) ) 
            { 
              while ((
    f.read()) != -1) { 
                switch(
    c) { 
                case 
    '&'
                  if (!
    marked) { 
                    
    f.mark(32); 
                    
    marked true
                  } else { 
                    
    marked false
                  } 
                  break; 
                case 
    ';'
                  if (
    marked) { 
                    
    marked false
                    
    System.out.print("(c)"); 
                  } else 
                    
    System.out.print((charc); 
                  break; 
                case 
    ' '
                  if (
    marked) { 
                    
    marked false
                    
    f.reset(); 
                    
    System.out.print("&"); 
                  } else 
                    
    System.out.print((charc); 
                  break; 
                default: 
                  if (!
    marked
                    
    System.out.print((charc); 
                  break; 
                } 
              } 
            } catch(
    IOException e) { 
              
    System.out.println("I/O Error: " e); 
            } 
          } 

    Ultima modifica di giannino1995; 28-10-2013 a 13:03

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    credo che la differenza sia chiara leggendo la javadoc
    BufferedInputStream

    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and resetmethods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and thereset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input


    BufferedReader


    Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.


    fonte:

    http://docs.oracle.com/javase/7/docs...redReader.html e
    http://docs.oracle.com/javase/7/docs...putStream.html
    RTFM Read That F*** Manual!!!

  3. #3
    Io non sono informatico e quelle definizioni non mi dicono nulla. A me interessava sapere quando è opportuno usare un metodo e quando l'altro dato che entrambi forniscono lo stesso risultato.
    A presto e grazie

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    Parti da un presupposto sbagliato: voler fare un programma in java non essendo un informatico e senza voler leggere e saper comprendere la documentazione.
    Entrambe forniscono lo stesso risultato, la differenza sta nel vedere come trattano i dati in ingresso, differenza che mette in chiara evidenza la documentazione.
    RTFM Read That F*** Manual!!!

  5. #5
    Sapresti spiegarmi con parole più semplici?

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    Sapresti spiegarmi con parole più semplici?
    Che il primo esempio è migliore e preferibile.

    <se vuoi comprendere>
    Perché? Perché il primo esempio usa dei *Reader che (insieme ai *Writer) sono "orientati" ai caratteri. Mentre *InputStream/*OutputStream sono "orientati" ai byte.
    Nel primo esempio f.read() legge caratteri, nel secondo esempio f.read() legge byte. Nel secondo usa s.getBytes() (che tra l'altro usa il charset della piattaforma) per ottenere dei byte dalla stringa. Se il charset è tale per cui vale l'equazione 1 carattere=1 byte, effettivamente non hai differenze. Se il charset fosse es. UTF-16, il secondo esempio sarebbe sballato e inappropriato. Punto.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    Grazie mille, quello che hai scritto lo trovo anche sul libro però non riesco a dimostrarlo. Come posso modificare il charset di java e come posso modificare l'oggetto stringa s per vedere il secondo codice andare a banane? Il charset è una cosa di windows o di java?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.