day3 25總結 位元組流

2021-07-11 14:07:32 字數 3591 閱讀 4647

返回讀到位元組,假如是-1表示已經讀到檔案尾

將位元組讀到buf陣列,一次最多讀buf.length個位元組,返回值為讀到的位元組數

a)write(bytedata);

一次只能寫乙個位元組

b)write(bytebuf);

一次最多寫buf.length個位元組

c)write(byte,intoffset,int length);

將buf陣列中的位元組,從offset位置開始寫,寫length個

static void writedata(string data)

throws ioexception{

outputstream out=null;

//1.構建輸出流物件

out=new fileoutputstream("data1.txt");

//out=new fileoutputstream("data1.txt",true);true

表示追加

//2.寫資料

out.write(data.getbytes());//byte

//3.釋放資源

out.close();

static void readdata() throws ioexception{

//1.構建fileinputstream流物件

inputstream in=new fileinputstream("data1.txt");

//2.讀資料

while(true){

int n=in.read();

if(n==-1)break;

system.out.println((char)n);

//3.釋放資源

in.close();

b)println(...);自動新增換行

printstream ps=new printstream(new fileoutputstream("m1.txt"));標準輸出流

ps.println("helloworld");

ps.println("tarena");

ps.close();

說明:使用

bufferedoutputstream

寫資料時,最後最好記得要重新整理(

flush)。

高效複製檔案:

static void copy03(file src,file dest)throws ioexception{

//1.構建流物件

inputstream in =new bufferedinputstream(new fileinputstream(src));

outputstream out =new bufferedoutputstream(new fileoutputstream(dest));

//2.讀寫資料

int len=-1;

byte buf=new byte[1024];//

long start=system.nanotime();

while((len=in.read(buf))!=-1){

out.write(buf,0,len);        

out.flush();記得要重新整理(

flush

//3.釋放資源

in.close();

out.close();

byte buf="helloworld".getbytes();

bytearrayinputstream in==new bytearrayinputstream(buf);

int n1=in.read();//每次讀乙個位元組

int n2=in.read();

system.out.println((char)n1);

system.out.println(n2);

in.close();

bytearrayoutputstream bos=new bytearrayoutputstream();

dataoutputstream out=new dataoutputstream(bos);

out.writeint(100);

out.writeint(200);

out.writechar('達');

out.writeutf("helloworld");寫字串必須用這個,帶編碼的

out.close();

datainputstream in=new datainputstream(newbytearrayinputstream(bos.tobytearray()));

int n1=in.readint();

int n2=in.readint();

char c=in.readchar();

string s=in.readutf();

讀字串必須用這個,帶編碼的

in.close();

system.out.println(n1+"/"+n2);

system.out.println(c);

system.out.println(s);

a)readobject()

a)writeobject(..)

class studentimplements serializable{

private static final long serialversionuid = -2135079723014730959l;僅僅是個標誌而已

string name;

/**transient

修飾的屬性不會被序列化

*/ transient int age;

student(string name,int age){

this.name=name;

this.age=age;

@override

public string tostring() {

return name+"/"+age;

public class objectstreamdemo01 {

public static void main(string args) throws ioexception, classnotfoundexception {

objectoutputstream out=new objectoutputstream(new fileoutputstream("o1.txt"));

out.writeobject("helloworld");

out.writeobject(new student("

紅武",12));

out.close();

objectinputstream in=new objectinputstream(new fileinputstream("o1.txt"));

object obj1=in.readobject();

object obj2=in.readobject();

system.out.println(obj1);

system.out.println(obj2);

in.close();

3 25 上午滬 深收盤總結

繼續昨天的跌勢,滬深兩市今日繼續低開,滬指一度下探3500的支撐,隨後 展開了逐級 盤中滬指一度收復3600點,深指一度翻紅,無奈權重股的再度殺跌,中石油 中石化 中國鋁業 中信 等 藍籌出現3 4個點的 嚴重拖累了 向上的走勢。而深指表現強於滬指,但在萬科等 的跳水後,也翻綠 此波 完全是由權重 ...

位元組對齊總結

寫在前面位元組對其是面試筆試當中出現頻率算是比較高的乙個知識點。在此總結一下。主要內容例題 pragma pack 2 class bu ubuf void foo typedef char f void enum disk bu 問 sizeof bu 的值是 首先先不急著回答上面的問題如果你不知...

位元組對齊總結

1.位元組對齊的緣由?一句話說就是提高訪問效率,訪問效率與機器相關,比如有的機器總是從偶數位元組開始取資料 同時,訪問效率也和資料型別相關,比如如果取乙個整型數字時,若能夠一次取出來肯定是最好,但是若由於機器取資料總是從偶數位元組開始這個原因花了兩次訪問,那麼肯定會降低效率了。2.字元對齊中的一些概...