byte[] buff = new byte[1024]; InputStream is; int c; c = is.read(buff, 0, 1024); System.out.println(new String(byte)); PrintWriter out = new PrintWriter( new OutputStreamWriter(new FileOutputStream("aa.txt"))); out.print(new String(byte));
import java.io.* ; public class TeeOutputStream extends OutputStream { OutputStream tee = null, out = null;
public TeeOutputStream(OutputStream chainedStream, OutputStream teeStream) { out = chainedStream;
if (teeStream == null) tee = System.out; else tee = teeStream; }
/** * Implementation for parent's abstract write method. * This writes out the passed in character to the both, * the chained stream and "tee" stream. */
public void write(int c) throws IOException { out.write(c);
tee.write(c); tee.flush(); }
/** * Closes both, chained and tee, streams. */ public void close() throws IOException { flush();
out.close(); tee.close(); }
/** * Flushes chained stream; the tee stream is flushed * each time a character is written to it. */ public void flush() throws IOException { out.flush(); }
/** Test driver */ public static void main( String args[]) throws Exception { FileOutputStream fos = new FileOutputStream("test.out"); TeeOutputStream tos = new TeeOutputStream(fos, System.out); PrintWriter pw = new PrintWriter(new OutputStreamWriter(tos));
pw.println("Testing line 1"); pw.println("Testing line 2"); pw.close(); } } 这是我在网上找的一个例子,它没有缓冲区大小的限制,可以少加改造,就可以满足需要;现在还有个问题和大家讨论,就是这个Tee类输出的是两个outputstream,如何把其中一个outputstream变为原来的inputstream传给其它得类进行处理?
InputStream is;
is.read(b,0,b.length);
System.out.write(b,0,length);
文件一样操作!
这个问题的实质就是如何多次使用一个inputstream?
InputStream is;
int c;
c = is.read(buff, 0, 1024);
System.out.println(new String(byte));
PrintWriter out = new PrintWriter( new OutputStreamWriter(new FileOutputStream("aa.txt")));
out.print(new String(byte));
try
{
//File fl = new File("c:\\cc\\c1.txt");
FileReader input=new FileReader("AlphaComposite.java");
FileWriter output=new FileWriter("temp2.txt");
BufferedReader br=new BufferedReader(input);
BufferedWriter bw=new BufferedWriter(output);
String s=br.readLine();
while(s!=null)
{
bw.write(s);
bw.newLine();
System.out.println(s);
s=br.readLine();
}
br.close();
bw.close(); }catch(IOException e){System.out.println(e);}
extends OutputStream
{
OutputStream tee = null, out = null;
public TeeOutputStream(OutputStream chainedStream,
OutputStream teeStream)
{
out = chainedStream;
if (teeStream == null)
tee = System.out;
else
tee = teeStream;
}
/**
* Implementation for parent's abstract write method.
* This writes out the passed in character to the both,
* the chained stream and "tee" stream.
*/
public void write(int c) throws IOException
{
out.write(c);
tee.write(c);
tee.flush();
}
/**
* Closes both, chained and tee, streams.
*/
public void close() throws IOException
{
flush();
out.close();
tee.close();
}
/**
* Flushes chained stream; the tee stream is flushed
* each time a character is written to it.
*/
public void flush() throws IOException
{
out.flush();
}
/** Test driver */
public static void main(
String args[]) throws Exception
{
FileOutputStream fos =
new FileOutputStream("test.out");
TeeOutputStream tos =
new TeeOutputStream(fos, System.out);
PrintWriter pw =
new PrintWriter(new OutputStreamWriter(tos));
pw.println("Testing line 1");
pw.println("Testing line 2");
pw.close();
}
}
这是我在网上找的一个例子,它没有缓冲区大小的限制,可以少加改造,就可以满足需要;现在还有个问题和大家讨论,就是这个Tee类输出的是两个outputstream,如何把其中一个outputstream变为原来的inputstream传给其它得类进行处理?