public class ByteStreamDemo {/*int available(); 可以取得輸入文件的大小(字節個數),沒有返回0void close(); 關閉輸入流abstract int read(); 讀取一個字節,并把讀到的字節返回,沒有返回-1int read(byte[] b); 將內容讀到byte數組,并且返回讀入的字節的個數。,沒有返回-1int read(byte[] b ,int off ,int len); 將內容讀到byte數組,從off開始讀,讀len個結束。,沒有返回-1*//*public void close(); 關閉輸出流。public void flush(); 刷新緩沖區。public void write(byte[] b); 將byte數組寫入數據流write(byte[] b ,int off ,int len); 將指定范圍的數據寫入數據流。public abstract void write(int b); 將一個字節數據寫入數據流*///輸入流沒有找到文件報異常,輸出流沒有文件會自動創建public static void executeByteFile(){File inFile = new File("D:"+File.separator+"intest.txt");File outFile = new File("D:"+File.separator+"outtest.txt");long start = System.currentTimeMillis();FileInputStream in = null;OutputStream out = null;try {in = new FileInputStream(inFile);//true表示在原文件上追加。out = new FileOutputStream(outFile,true);byte[] inb = new byte[1024];int size = 0;while ((size = in.read(inb)) != -1) { // System.out.println(new String(inb,0,size,"gbk"));out.write(inb, 0, size);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(out);CloseUtil.close(in);}System.out.println("結束時間:"+(System.currentTimeMillis() - start));}/**操作字節數組的輸入流* @throws IOException */public static void executeByteIn() throws IOException{final String str = "我愛中華!";byte[] bytes = str.getBytes();ByteArrayInputStream byin = new ByteArrayInputStream(bytes);byte[] db = new byte[3];int read = byin.read(db);while ( read !=-1 ) {String s = new String(db,0,read,"UTF-8");System.out.println(s);read = byin.read(db);}byin.close();}/**操作字節數組的輸出流* @throws IOException */public static void executeByteOut() throws IOException{final String str1 = "我愛中華!";final String str2 = "字節數組輸出流!";byte[] bytes1 = str1.getBytes();byte[] bytes2 = str2.getBytes();ByteArrayOutputStream out = new ByteArrayOutputStream();out.write(bytes1);out.write(bytes2);//先把數據都寫進字節數組輸出流中。之后統一輸出 System.out.println(out.toString());out.close();}/** 管道流*/public static void executePip() throws IOException{PipedByteOut out = new PipedByteOut();PipedByteIn in = new PipedByteIn();out.getOut().connect(in.getIn());Thread inThread = new Thread(in,"thread-piped-in");Thread outThread = new Thread(out,"thread-piped-out");inThread.start();outThread.start();}/** 緩存字節流*/public static void executeBufferStream() throws Exception{File inFile = new File("D:"+File.separator+"intest.txt");File outFile = new File("D:"+File.separator+"outtest.txt");long start = System.currentTimeMillis();BufferedInputStream inbuffer = new BufferedInputStream(new FileInputStream(inFile));BufferedOutputStream outbuffer = new BufferedOutputStream(new FileOutputStream(outFile,true));byte[] inb = new byte[1024];int size = 0;while ((size = inbuffer.read(inb)) != -1) {outbuffer.write(inb, 0, size);}outbuffer.flush();CloseUtil.close(outbuffer);CloseUtil.close(inbuffer);System.out.println("結束時間:"+(System.currentTimeMillis() - start));}/** 對象流可以將對象序列化*/public static class SerializeUtil{public static byte[] serializeObject(Object object){ObjectOutputStream out = null;ByteArrayOutputStream by = new ByteArrayOutputStream();try {out = new ObjectOutputStream(by);out.writeObject(object);return by.toByteArray();} catch (IOException e) {throw new RuntimeException("對象序列化錯誤");}finally{CloseUtil.close(by);CloseUtil.close(out);}}public static <T> T unSerialized(byte[] by){if(by == null || by.length == 0) return null;ByteArrayInputStream byin = null;ObjectInputStream in = null;try {byin = new ByteArrayInputStream(by);in = new ObjectInputStream(byin);return (T) in.readObject();} catch (IOException | ClassNotFoundException e) {throw new RuntimeException("對象反序列化錯誤");}finally{CloseUtil.close(in);CloseUtil.close(byin);}}}public static void main(String[] args) throws Exception{User user = new User();user.setAddres("地址");user.setId(1);user.setName("測試");byte[] bs = SerializeUtil.serializeObject(user);Object object = SerializeUtil.unSerialized(bs);System.out.println(object);} }class PipedByteIn implements Runnable{private PipedInputStream in = new PipedInputStream();@Overridepublic void run() {try {byte[] by = new byte[1024];int i = in.read(by);while (i != -1) {System.out.println(new String(by,0,i,"UTF-8"));i = in.read(by);}} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(in);}}public PipedInputStream getIn(){return in;} }class PipedByteOut implements Runnable{private PipedOutputStream out = new PipedOutputStream();@Overridepublic void run() {byte[] bytes = "我愛中華!".getBytes();try {out.write(bytes);} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(out);}}public PipedOutputStream getOut(){return out;} }class User implements Serializable{private static final long serialVersionUID = 1L;private Integer id;private String name;//transient代表該字段不被序列化private transient String addres;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddres() {return addres;}public void setAddres(String addres) {this.addres = addres;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", addres=" + addres + "]";} } class CloseUtil{public static void close(Closeable closeable){if(closeable != null){try {closeable.close();} catch (IOException e) {e.printStackTrace();}}} }
轉載于:https://www.cnblogs.com/DivineHost/p/5387807.html