日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习笔记之 IO包 字节流

發布時間:2023/12/4 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习笔记之 IO包 字节流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IO包最重要的五個類和一個接口

File/OutputStream/InputStream(字節流)/Writer/Reader(字符流)

一個接口:Serializable

?

File類:



字節流:

OutputStreamInputStream是抽象類

OutputStream是整個IO包中,字節輸出流的最大類

FileOutputStream是一個子類,通常用整個子類通過多態實例化OutputStream


?


?

?


packageiotest;

?

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.io.OutputStream;

importjava.io.FileOutputStream;

importjava.io.InputStream;

importjava.io.FileInputStream;

?

publicclass OutputStreamDemo1 {

public static void main(String args[]) throws Exception{

File f =newFile("D:"+File.separator+"javasrc"+File.separator+"write_file.txt");

OutputStream o=null;

o=new FileOutputStream(f);

String s="Hello World!";

byte b[]=s.getBytes();

o.write(b);

o.close();

//修改文件 追加文件內容

OutputStream o1=null;

o1=new FileOutputStream(f,true);

o1.write("\r\n".getBytes()); //增加換行符

o1.write(b);

o1.close();

?

//讀取并打印文件

//01 定義一個字節數組 長度為1024

InputStream in1=null;

in1=new FileInputStream(f);

byte b1[]=new byte[1024];

int len=in1.read(b1);

in1.close();????????????????

print(b1,len);

?

//02 定義一個字節數組 長度為文件長度

InputStream in2=null;

in2=new FileInputStream(f);

byte b2[]=new byte[(int)f.length()];

in2.read(b2);

in2.close();

print(b2);

?

?? ????//03 已經文件長度 逐個字節讀取

?????? ???????InputStream in3=null;

in3=new FileInputStream(f);

byte b3[]=new byte[(int)f.length()];

for (int i=0;i<b3.length;i++){

b3[i]=(byte) in3.read();

}

in3.close();

print("the third read method:");

print(b3);

?????

//04 未知文件長度讀取

InputStream in4=null;

in4=new FileInputStream(f);

byte b4[]=new byte[1024];

int i=0;

int temp=0;

while ((temp=in4.read())!=-1){

b4[i]=(byte)temp;

i++;

}

in4.close();

print("the fourth read method:");

print(b4);

}

?

public static void print(byte b[],int len){

System.out.println(new String(b,0,len));

}

public static void print(byte b[]){

System.out.println(new String(b));

}

public static void print(String b){

System.out.println(b);

}

?

}

總結

以上是生活随笔為你收集整理的Java学习笔记之 IO包 字节流的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。