02-06
PipedOutputStream
PipedInputStream
在应用程序中创建管道通信。字节类型数据的读写
例子:
import java.io.IOException;
import java.io.PipedOutputStream;
public class Sender extends Thread {
private PipedOutputStream out=new PipedOutputStream();
public PipedOutputStream getOutputStream(){
return out;
}
public void run(){
String strInfo=new String("hello,receiver!");
try {
out.write(strInfo.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Receiver类:
import java.io.IOException;
import java.io.PipedInputStream;
public class Receiver extends Thread {
private PipedInputStream in=new PipedInputStream();
public PipedInputStream getInputStream(){
return in;
}
public void run(){
byte[] buf=new byte[1024];
try {
int len=in.read(buf);
System.out.println("the following message comes from sender:/n"+
new String(buf,0,len));
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试类:
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamTest {
public static void main(String[] args) throws IOException {
Sender t1=new Sender();
Receiver t2=new Receiver();
PipedOutputStream out=t1.getOutputStream();
PipedInputStream in=t2.getInputStream();
in.connect(out);
t1.start();
t2.start();
}
}
ByteArrayInputStream
ByteArrayOutputStream
字节数组的读写,支持虚拟文件和内存映像
例子:把输入流中的英文小写字符转换成大写,并输出
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteArrayTest {
public static void main(String[] args) {
String tmp="sdfwerdfgfgeretcf";
byte[] src=tmp.getBytes();
ByteArrayInputStream in=new ByteArrayInputStream(src);
ByteArrayOutputStream out= new ByteArrayOutputStream();
transform(in,out);
byte [] results=out.toByteArray();
System.out.println(new String(results));
}
public static void transform(InputStream in,OutputStream out){
int ch=0;
try {
while((ch=in.read())!=-1){
int upperCh=Character.toUpperCase(ch);
out.write(upperCh);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
StringReader StringWriter对应字符串处理
重视IO程序代码的复用:
System.in 对应键盘输入,System..out 对应屏幕显示输出
InputStream的read()方法返回-1表示输入流的结束
在windows下ctrl+Z表示输入流的结束,Linux下ctrl+D表示输入流的结束.
02-07-08-09-10
正如张孝祥老师所讲,字符编码:感觉有点晕,编码知识点还需要下点功夫继续学习一下
Unicode
UTF-8
UTF-16
字符编码的操作体验
例子1:
public class CharCode {
public static void main(String[] args) {
String strChina="李德国";
for(int i=0;i<strChina.length();i++){
System.out.println(Integer.toHexString((int)strChina.charAt(i)));
}
byte [] buf=strChina.getBytes();
for(int i=0;i<buf.length;i++){
System.out.println(Integer.toHexString(buf[i]));
}
for(int i=0;i<buf.length;i++){
System.out.write(buf[i]);
}
System.out.println();
}
}
例子2:
import java.io.IOException;
public class CharDecode {
public static void main(String[] args) throws IOException {
System.out.println("please enter a chinese string");
byte[] buf=new byte[1024];
String strInfo=null;
int pos=0;
int ch=0;
while(true){
ch=System.in.read();
System.out.println(Integer.toHexString(ch));
switch(ch){
case '/r':
break;
case '/n':
strInfo=new String(buf,0,pos);
for(int i=0;i<strInfo.length();i++){
System.out.println(Integer.toHexString((int)strInfo.charAt(i)));
}
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}