字节流的 创建 写入文字 复制文件
// 字節(jié)流文件的創(chuàng)建?? 復(fù)制
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();
?? ??? ?}
?? ?}
?
?
?
?? ?
?? ?/*
?? ? * 在文件中續(xù)寫文件
?? ? */
?? ?
?? ?public static void fun() throws Exception{
//?? ??? ?在ac.txt中續(xù)寫文文字
?? ??? ? FileOutputStream fos=new FileOutputStream("D:\\IO\\ac.txt",true);
//?? ??? ? 寫入字符串? 加getBytes()
?? ??? ?fos.write("\r\n 你好\r\n".getBytes());
//?? ??? ??? ?加入字?jǐn)?shù)組
?? ??? ?byte[] b={66,67,68,69};
//?? ?? ? ?寫入數(shù)組
?? ?? ? ?fos.write(b);
//?? ??? ?關(guān)閉
?? ? ?? ?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];
//?? ??? ?定義長(zhǎng)度為零
?
?? ??? ?int len=0;
//?? ??? ? 如果長(zhǎng)度為-1 的時(shí)候結(jié)束
?? ??? ?
?? ??? ? while((len=fis.read(b))!=-1){
//?? ??? ??? ?打印文件內(nèi)容同
?? ??? ??? ?System.out.println(new String (b,0,len));
?? ??? ?}
?? ??? ?
?? ??? ?fis.close();
?? ??? ?
?? ?}
?? ?
?
?
?? ?
//?? ?復(fù)制文件
?? ?
?? ?public static void fun2() throws Exception{
//?? ??? ?要復(fù)制的文件
?? ??? ?FileInputStream fis=new FileInputStream("d:\\IO\\ac.txt");
//?? ??? ?要復(fù)制到哪里的路徑
?? ??? ?FileOutputStream fos=new FileOutputStream("D:\\IO\\ac1.txt");
//?? ??? ?復(fù)制的速度為2M
?? ??? ?byte [] b=new byte[1024];
//?? ? ?? ? 定義長(zhǎng)度為零
?? ??? ?int len=0;
?
//?? ??? ?如果長(zhǎng)度為-1 的時(shí)候結(jié)束
?
?? ??? ?while ((len=fis.read(b))!=-1){
?
?? ??? ??? ?fos.write(b,0,len);
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?fos.close();
?? ??? ?fis.close();
?? ?}
}
轉(zhuǎn)載于:https://www.cnblogs.com/hph1728390/p/10596228.html
總結(jié)
以上是生活随笔為你收集整理的字节流的 创建 写入文字 复制文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OO第一单元总结博客
- 下一篇: CF1097D Makoto and a