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

//     字节流文件的创建   复制

    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(" 你好 ".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