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 © not.\n";
char buf[] = new char[s.length()];
s.getChars(0, s.length(), buf, 0);
CharArrayReader in = new CharArrayReader(buf);
int c;
boolean marked = false;
try ( BufferedReader f = new BufferedReader(in) )
{
while ((c = 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((char) c);
break;
case ' ':
if (marked) {
marked = false;
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (!marked)
System.out.print((char) c);
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 © 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 ((c = 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((char) c);
break;
case ' ':
if (marked) {
marked = false;
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (!marked)
System.out.print((char) c);
break;
}
}
} catch(IOException e) {
System.out.println("I/O Error: " + e);
}
}
}