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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java字节的输入输出流,java 字节输入输出流

發布時間:2025/4/5 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java字节的输入输出流,java 字节输入输出流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標簽:/*

字節流:

InputStream

OutputStream

*/

import java.io.*;

class FileStream

{

public static void main(String[] args) throws IOException

{

outputFile();

//inputFile_1();

inputFile_2();

}

/*不利用數組進行緩沖的方法*/

public static void outputFile() throws IOException //寫

{

FileOutputStream fos=new FileOutputStream("D:\\myfile\\mycode\\4.txt");

fos.write("abndks你好".getBytes());

fos.close();

}

public static void inputFile() throws IOException//讀

{

FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");

int ch=0;

while((ch=fis.read())!=-1)

{

System.out.println((char)ch);

}

fis.close();

}

/*利用數組進行緩沖的方法進行讀*/

public static void inputFile_1() throws IOException//讀

{

FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");

int num=0;

byte[] b=new byte[1024];

while((num=fis.read(b))!=-1)

{

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

}

fis.close();

}

/*使用字節流對象中特有的available獲取字節個數。*/

public static void inputFile_2() throws IOException//讀

{

FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");

int num=fis.available();

byte[] b=new byte[num];

int x=fis.read(b);

System.out.println("x:"+x);

System.out.println("num:"+num);

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

fis.close();

}

}

復制圖片:

/*

拷貝圖片:

思路:

1,用字節讀取流對象和圖片關聯。

2,用字節寫入流對象創建一個圖片文件,用于存儲獲取到的圖片數據。

3,通過循環讀寫,完成數據的存儲。

4,關閉資源。

*/

import java.io.*;

class CopyPic

{

public static void main(String[] args)

{

FileInputStream fis=null;

FileOutputStream fos=null;

try

{

System.out.println("1111");

fis=new FileInputStream("D:\\20.jpg");

System.out.println("2222");

fos=new FileOutputStream("C:\\test\\new.jpg");

System.out.println("3333");

byte[] b=new byte[1024];

int len=0;

while((len=fis.read(b))!=-1)

{

fos.write(b,0,len);

}

}

catch (IOException e)

{

throw new RuntimeException("復制文件失敗");

}

finally

{

try

{

if(fis!=null)

fis.close();

}

catch (IOException e)

{

throw new RuntimeException("讀取關閉失敗");

}

try

{

if(fos!=null)

fos.close();

}

catch (IOException e)

{

throw new RuntimeException("寫入關閉失敗");

}

}

}

}

復制MP3文件,使用字節流緩沖區

/*

通過復制MP3媒體文件來使用緩沖區

*/

import java.io.*;

class CopyMp3

{

public static void main(String[] args)

{

long start=System.currentTimeMillis();

copy_1();

long end=System.currentTimeMillis();

System.out.println((end-start)+"毫秒");

}

public static void copy_1()

{

FileInputStream fis=null;

FileOutputStream fos=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try

{

fis=new FileInputStream("D:\\CloudMusic\\Lonely.mp3");

fos=new FileOutputStream("D:\\myfile\\mycode\\new.mp3");

bis=new BufferedInputStream(fis);

bos=new BufferedOutputStream(fos);

int by=0;

while((by=bis.read())!=-1)

{

bos.write(by);

}

}

catch (IOException e)

{

throw new RuntimeException("復制失敗");

}

finally

{

try

{

if(bis!=null)

bis.close();

}

catch (IOException e)

{

throw new RuntimeException("讀取流關閉失敗");

}

try

{

if(bos!=null)

bos.close();

}

catch (IOException e)

{

throw new RuntimeException("寫入流關閉失敗");

}

}

}

}

標簽:

總結

以上是生活随笔為你收集整理的java字节的输入输出流,java 字节输入输出流的全部內容,希望文章能夠幫你解決所遇到的問題。

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