fileWriter /fileReader 对文件进行读写
示例代码:
FileWriter out=new FileWriter("test.txt");
out.write("张三");
out.close();
char[] buf=new char[1024];
FileReader in=new FileReader("test.txt");
int len=in.read(buf);
System.out.println(new String(buf,0,len));
StringWriter/StringReader 对内存里的位置进行读写
示例代码如下:
try {
throw new Exception("test");
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
System.out.println(e.getMessage());
// System.out.println(sw.toString());
}
PipedWriter/PipedReader 两个线程间进行通讯
8位对应InputStream和OutputStream
fileInputStream/FileOutputStream 对文件进行读写
示例代码:
public static void main(String[] args) throws Exception
{
FileOutputStream out=new FileOutputStream("test.txt");
out.write("张三".getBytes());
byte[] buf=new byte[1024];
File f=new File("test.txt");
FileInputStream in=new FileInputStream(f);
int len=in.read(buf);
System.out.println(new String(buf,0,len));
in.close();
}
ByteArrayInputStream/ByteArrayOutputStream 对字节数组进行读写
示例代码:
public static void main(String[] args)
{
String tmp="abc";
byte[] src=tmp.getBytes();
ByteArrayInputStream input=new ByteArrayInputStream(src);
ByteArrayOutputStream output=new ByteArrayOutputStream();
transform(input,output);
byte[] result=output.toByteArray();
System.out.println(new String(result));
transform(System.in,System.out);
}
public static void transform(InputStream in,OutputStream out)
{
int ch=0;
try
{
while((ch=in.read())!=-1)
{
int upperCh=(int)(Character.toUpperCase((char)ch));
out.write(upperCh);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
PipedInputStream/PipOutputStream 线程间通讯使用
示例代码:
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 (Exception e) {
e.printStackTrace();
}
}
}
class Receiver extends Thread {
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getInputStream() {
return in;
}
public void run() {
String strInfo = new String("hello,receiver!");
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 (Exception e) {
e.printStackTrace();
}
}
}
class PipedStreamTest {
public static void main(String[] args) throws Exception {
Sender t1 = new Sender();
Receiver t2 = new Receiver();
PipedOutputStream out = t1.getOutputStream();
PipedInputStream in = t2.getInputStream();
out.connect(in);
t1.start();
t2.start();
}
}
bufferedInputStream/bufferedOutputStream
示例代码:
public static void main(String[] args) throws Exception
{
FileOutputStream fos=new FileOutputStream("count.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos);
dos.writeUTF("ab中国");
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();
FileInputStream fis=new FileInputStream("count.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
System.out.println(dis.readUTF());
byte[] buf=new byte[1024];
int len=dis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();
}