IO管道流
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Input input = new Input(in);
Output output = new Output(out);
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
class Input implements Runnable{
private PipedInputStream in;
Input(PipedInputStream in){
this.in = in;
}
@Override
public void run() {
try {
byte[] buf = new byte[1024];
int len = in.read(buf);
String str = new String(buf,0,len);
System.out.println(str);
in.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
class Output implements Runnable{
private PipedOutputStream out;
Output(PipedOutputStream out){
this.out = out;
}
@Override
public void run() {
try {
out.write("this is 啥".getBytes());
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
结果:
this is 啥
基本数据流
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// write();
read();
}
public static void read() throws IOException{
DataInputStream dis = new DataInputStream(
new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt"));
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
public static void write() throws IOException{
DataOutputStream dos = new DataOutputStream(
new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt"));
dos.writeUTF("这是啥this");
dos.close();
}
}