Java基礎突擊第九天0022(內存操作流,管道流,打印流)

寫入內存 內存輸入流:ByteArrayInputStream

從內存讀出 內存輸出流:ByteArrayOutputStream

內存操作流一般在生成一些臨時信息時纔會使用。代碼執行後臨時信息會被刪除。

public ByteArrayInputStream(byte[] b)

public ByteArrayInputStream(byte[] b,int begin,int length)

public ByteArrayOutputStream()

public void write(int b)


import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				String str = "HELLOWORLD!";
				ByteArrayInputStream bi = null;
				ByteArrayOutputStream bo = null;
				bi = new ByteArrayInputStream(str.getBytes());
				bo = new ByteArrayOutputStream();
				int tempRec = 0;
				while((tempRec = bi.read())!=-1){
						char tempRecChar = (char)tempRec;
						bo.write(Character.toLowerCase(tempRecChar));
				}
				String info = bo.toString();
				try{
						bi.close();
						bo.close();
				}catch(IOException e){
						e.printStackTrace();
				}
				System.out.println(info);
		}//main
}


管道流(進程間的通信)

發出信息(PipedOutputStream)

接收信息(PipedInputStream)

import java.io.IOException;
import java.io.PipedOutputStream;
import java.io.PipedInputStream;
public class TestJava{
}
class Send implements Runnable{
		private PipedOutputStream po = null;
		public Send(){
				this.po = new PipedOutputStream();
		}
		public void run(){
				String str = "Hello World!";
				try{
						this.po.write(str.getBytes());
				}catch(IOException e){
						e.printStackTrace();
				}
		}
		public PipedOutputStream getPo(){
				return this.po;
		}
}//send class
class Receive implements Runnable{
		private PipedInputStream pi = null;
		public Receive(){
				this.pi = new PipedInputStream();
		}
		public void run(){
				byte[] bReady = new byte[1024];
				int lenReady = 0;
				try{
						lenReady = this.pi.read(bReady);
				}catch(IOException e){
						e.printStackTrace();
				}
				System.out.println("Content:"+new String(bReady,0,lenReady));
		}
		public PipedInputStream getPi(){
				return this.pi;
		}
}//receive class
class Demo{
		public static void main(String[] args) throws Exception{
				Send s = new Send();
				Receive r = new Receive();
				try{
						s.getPo().connect(r.getPi());
				}catch(IOException e){
						e.printStackTrace();
				}
				new Thread(s).start();
				new Thread(r).start();
		}//main
}

output:    Content:Hello World!

在操作時只需要使用PipedOutputStream類中的connect方法就能將兩個管道線程連接在一起。

線程啓動後會自動進行管道的輸入輸出操作。


打印流:

PrintStream是OutputStream的子類。

構造:

public PrintStream(File file) throws FileNotFoundException

public PrintStream(OutputStream out)

普通:

public PrintStream printf(Locale l,String format,Object...args) //根據指定Locale進行格式化輸出

public PrintStream printf(String format,Object...args)    //根據本地環境進行格式化輸出

public void println(boolean b)

public void print(boolean b)

有一個PrintStream的構造方法可以接收OutputStream的子類,所以包裝OutputStream後用PrintStream進行輸出更爲方便。

而且PrintStream也可以像C語言一樣進行格式化輸出。%d %c % f %s,前三個都可以用s%輸出

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.OutputStream;

public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				OutputStream ou = new FileOutputStream(new File("C:"+File.separator
					+"JavaStudy"+File.separator+"JavaIO"+File.separator+"test05.txt"));
				PrintStream pts = new PrintStream(ou);
				pts.print("Hello ");
				pts.print("World!");
				pts.print("\r\n2+2="+4);
				String name = "FangXy";
				int age = 24;
				float score = 65.0f;
				char gender = 'M';
				pts.printf("\r\nName:%s,Age:%d,Score:%f,Gender:%c",name,age,score,gender);
				pts.close();
		}//main
}

執行後文件中:

Hello World!
2+2=4
Name:FangXy,Age:24,Score:65.000000,Gender:M

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章