日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

字节流的 创建 写入文字 复制文件

發布時間:2025/7/14 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字节流的 创建 写入文字 复制文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//     字節流文件的創建?? 復制

    import java.io.*;

      import javax.imageio.stream.FileImageInputStream;


      public class FileL {

?      ? ?public static void main(String[] args) {

//      異常處理
?? ???       ?try {
?? ??? ???       ?fun();
?? ??? ???       ?fun1();
?? ??? ???       ?fun2();
?? ???       ?} catch (Exception e) {
?? ??? ???      
?? ??? ???       ?e.printStackTrace();
?? ??? ?}

?? ?}

?

?

?


?? ?
?? ?/*
?? ? * 在文件中續寫文件
?? ? */
?? ?
??     ?public static void fun() throws Exception{


//?? ???       ?在ac.txt中續寫文文字


?? ??? ?   FileOutputStream fos=new FileOutputStream("D:\\IO\\ac.txt",true);


//?? ???    ? 寫入字符串? 加getBytes()


?? ???     ?fos.write("\r\n 你好\r\n".getBytes());


//?? ???     ??? ?加入字數組


?? ???     ?byte[] b={66,67,68,69};


//?? ??        ? ?寫入數組


?? ??    ? ?fos.write(b);


//?? ???       ?關閉


?? ?    ?? ?fos.close();
?? ?}

?

?

?

?


/*
?*?? ??? ???     ? 讀取ac.txt文件


??? ?*/?? ?
?    ? ?public static void fun1() throws Exception{


//?? ??? ?      要讀取的文件


?? ???   ?FileInputStream fis=new FileInputStream("D:\\IO\\ac.txt");


//?? ??? ?      用2M的速度讀取


?? ???     ?byte [] b=new byte[1024*10];


//?? ???     ?定義長度為零

?


?? ???     ?int len=0;


//?? ???  ? 如果長度為-1 的時候結束
?? ??? ?
?? ??? ?    while((len=fis.read(b))!=-1){


//?? ??? ??? ?打印文件內容同


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


?? ??? ?}
?? ??? ?
?? ??? ?fis.close();
?? ??? ?
?? ?}
?? ?

?

?


?? ?
//?? ?復制文件
?? ?
??     ?public static void fun2() throws Exception{


//??           ??? ?要復制的文件


?? ???       ?FileInputStream fis=new FileInputStream("d:\\IO\\ac.txt");


//?? ???           ?要復制到哪里的路徑


?? ???       ?FileOutputStream fos=new FileOutputStream("D:\\IO\\ac1.txt");


//?? ???         ?復制的速度為2M


?? ???       ?byte [] b=new byte[1024];


//?? ?      ?? ?  定義長度為零


?? ???           ?int len=0;

?


//?? ??? ?如果長度為-1 的時候結束

?


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

?


?? ??? ??? ?fos.write(b,0,len);
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?fos.close();


?? ??? ?fis.close();
?? ?}
}

轉載于:https://www.cnblogs.com/hph1728390/p/10596228.html

總結

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

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