java.io.stream
1. package com.io.Stream; import java.io.*; public class NyFileInputStream1 { /** * 读取文件的streamIO * * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new NyFileInputStream1(); } public NyFileInputStream1(){ InputStream fis=null; FileOutputStream fos=null; try { fis=new FileInputStream(new File("D:\\Zh.java")); //fis=new ByteArrayInputStream("wo hao 周海".getBytes()); fos=new FileOutputStream(new File("D:\\Zhouhai.txt"),true); int len=0; byte bytes[]=new byte[1024]; while( (len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); System.out.println(len); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 2. package com.io.Stream; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; /**向管道输出流 写数据的线程 * @param args */ public class PipedSender extends Thread{ private PipedOutputStream out=new PipedOutputStream(); public PipedOutputStream getPipedOutputStream() { return out; } public void run(){ try { for(int i=-127;i<=128;i++){ out.write(i); this.yield(); } } catch (Exception e) { // TODO: handle exception }finally{ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { // TODO Auto-generated method stub PipedSender pipedSender=new PipedSender(); PipedReceiver pipedReceiver=new PipedReceiver(pipedSender); pipedSender.start(); pipedReceiver.start(); } } //从管道输入流 读取数据的线程 class PipedReceiver extends Thread{ private PipedInputStream in; public PipedReceiver(PipedSender pipedSender){ try { in=new PipedInputStream(pipedSender.getPipedOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void run(){ try{ int data; while((data=in.read()) != -1){ System.out.println(data); } in.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } 3. package com.io.Stream; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Stream { /** * 怎么向文件中写Strting 又读成String * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Stream(); } public Stream(){ try { FileOutputStream out=new FileOutputStream("D:/Stream.txt",true); //读取我要的String ByteArrayInputStream in=new ByteArrayInputStream("zhou hai周省劲".getBytes()); int len=in.available(); //获取所有的字节数目 byte[] bytes=new byte[len]; in.read(bytes); //把输入流的写bytes数组中 out.write(bytes, 0, len); out.close(); in.close(); FileInputStream fis=new FileInputStream("D:/Stream.txt"); int len2=fis.available(); byte byin[]=new byte[len2]; fis.read(byin); System.out.println(new String(byin)); fis.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 4. package com.io.Stream; import java.io.*; public class ByteArrayOutputStreamTester { /** * byteArrayOutputStream的运用 //字节数据输出流 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ByteArrayOutputStream out=new ByteArrayOutputStream(); try { out.write("周海".getBytes("utf-8")); //用uft-8的编码方式写到数组中 byte[] buff=out.toByteArray(); //获取字节数组 out.close(); ByteArrayInputStream in=new ByteArrayInputStream(buff); int len=in.available(); byte[] buffin=new byte[len]; in.read(buffin); //把 buff字节数组的数据读入到 buffiin中 in.close(); System.out.println(new String(buffin,"utf-8")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 5. package com.io.Stream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; public class FormatDateIO { /** * 可以读取基本的数据类型的IO * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { FileOutputStream out1=new FileOutputStream("D:/Zhou.txt"); BufferedOutputStream out2=new BufferedOutputStream(out1); DataOutputStream out=new DataOutputStream(out2); out.writeByte(-12); out.writeLong(12); out.writeChar('1'); out.writeUTF("周"); out.close(); InputStream in1=new FileInputStream("D:/Zhou.txt"); BufferedInputStream in2=new BufferedInputStream(in1); DataInputStream in=new DataInputStream(in2); System.out.println(in.read() +" "+in.readLong()+" "+in.readChar()+" " +in.readUTF()); in.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }