张森霖 10-13

IO流:

字节输入输出流:

  • inputstream类的常用子类有FileinputStream,用于从文件中读取数据。

  • output stream类的常用子类有Fi了output Stream,用于向文件写数据。

  • 复制文件步骤,1、用file读取文件名,以及复制目的地。2、判断文件是否存在 3、开启输入输出流 4、定义缓冲数组。5、读取字节流 输出字节流。6、关闭流。

    public static void main(String[] args) throws IOException{
        File srFile=new File("D:\\迅雷下载\\Adobe-Premiere-Pro-CC2019.rar");
    File targetFile=new File("G:\\",srFile.getName());
    if(srFile.exists()&&srFile.isFile()) {
    int n;
    InputStream in=new FileInputStream(srFile);
    OutputStream out=new FileOutputStream(targetFile);
    byte[] bytes=new byte[1<<20];
    while((n=in.read(bytes))!=-1) {
    out.write(bytes, 0, n);
    }
    out.flush();
    out.close();
    in.close();
  • 在创建输入输出流时,可能会出现异常,如:输出流试图要写入的文件可能不允许操作或有其他受限等原因。所以要用try-catch 或者将异常往上抛出。

今日总结:今天学习了字节流,这部分比较简单,相对来说容易懂。

标签

评论


© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1