当前位置: 技术问答>java相关
谁能给我一个PipedInputStream and PipedOutputStream 怎么用的例子,我想用数据库image字段对另一个imaeagezi字段
来源: 互联网 发布时间:2015-09-15
本文导语: 多谢 | 这是Sun的一个例子,看看吧。 RhymingWords.java: import java.io.*; class RhymingWords { public static void main(String[] args) { try { DataInputStream words = new DataIn...
多谢
|
这是Sun的一个例子,看看吧。
RhymingWords.java:
import java.io.*;
class RhymingWords {
public static void main(String[] args) {
try {
DataInputStream words = new DataInputStream(new FileInputStream("words.txt"));
// do the reversing and sorting
InputStream rhymedWords = reverse(sort(reverse(words)));
// write new list to standard out
DataInputStream dis = new DataInputStream(rhymedWords);
String input;
while ((input = dis.readLine()) != null) {
System.out.println(input);
}
dis.close();
} catch (Exception e) {
System.out.println("RhymingWords: " + e);
}
}
public static InputStream reverse(InputStream source) {
PipedOutputStream pos = null;
PipedInputStream pis = null;
try {
DataInputStream dis = new DataInputStream(source);
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
PrintStream ps = new PrintStream(pos);
new WriteReversedThread(ps, dis).start();
} catch (Exception e) {
System.out.println("RhymingWords reverse: " + e);
}
return pis;
}
public static InputStream sort(InputStream source) {
PipedOutputStream pos = null;
PipedInputStream pis = null;
try {
DataInputStream dis = new DataInputStream(source);
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
PrintStream ps = new PrintStream(pos);
new SortThread(ps, dis).start();
} catch (Exception e) {
System.out.println("RhymingWords sort: " + e);
}
return pis;
}
}
RhymingWords.java:
import java.io.*;
class RhymingWords {
public static void main(String[] args) {
try {
DataInputStream words = new DataInputStream(new FileInputStream("words.txt"));
// do the reversing and sorting
InputStream rhymedWords = reverse(sort(reverse(words)));
// write new list to standard out
DataInputStream dis = new DataInputStream(rhymedWords);
String input;
while ((input = dis.readLine()) != null) {
System.out.println(input);
}
dis.close();
} catch (Exception e) {
System.out.println("RhymingWords: " + e);
}
}
public static InputStream reverse(InputStream source) {
PipedOutputStream pos = null;
PipedInputStream pis = null;
try {
DataInputStream dis = new DataInputStream(source);
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
PrintStream ps = new PrintStream(pos);
new WriteReversedThread(ps, dis).start();
} catch (Exception e) {
System.out.println("RhymingWords reverse: " + e);
}
return pis;
}
public static InputStream sort(InputStream source) {
PipedOutputStream pos = null;
PipedInputStream pis = null;
try {
DataInputStream dis = new DataInputStream(source);
pos = new PipedOutputStream();
pis = new PipedInputStream(pos);
PrintStream ps = new PrintStream(pos);
new SortThread(ps, dis).start();
} catch (Exception e) {
System.out.println("RhymingWords sort: " + e);
}
return pis;
}
}