//Used buffered input.
import java.io.*;
class BufferedInputStreamDemo{
public static void main(String[] args) throws IOException{
String s = "This is a © copyright symbol but this is © not./n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
int c;
boolean marked = false;
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;
}
}
}
}
import java.io.*;
class BufferedInputStreamDemo{
public static void main(String[] args) throws IOException{
String s = "This is a © copyright symbol but this is © not./n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
int c;
boolean marked = false;
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;
}
}
}
}
本文展示了一个使用Java中的BufferedInputStream处理特定字符序列的例子,通过标记和重置输入流来实现对特殊符号的识别与替换,适用于字符处理与流操作的学习。
 示例&spm=1001.2101.3001.5002&articleId=819302&d=1&t=3&u=e54afd33c3bb4840b8421bbd71294b8f)
1436

被折叠的 条评论
为什么被折叠?



