JAVA 二十六 IO流,字节流

JAVA 二十六 IO流,字节流

标签: JAVA


字节流文件操作

import java.io.File;
import java.io.IOException;

public class FileDemo {
    /*
     * createNewFile 在指定目录下创建一个文件,如果该文件已经存在,创建失败
     * mkdir 创建一个文件夹
     * mkdirs 创建多级文件夹
     * 2 判断
     * isFile 判断文件是否存在  
     * isDirectory 判断文件夹是否存在
     * 3 获取
     * getName 获取文件的名字
     * getAbsolutePath 获取文件的绝对路径
     * getPath 获取文件的路径
     * getParent 获取文件的上级目录
     * length 获取文件的长度
     * listFiles 获取指定文件夹下所有的文件列表
     * 4 删除
     * delete 删除对应的文件
     * deleteOnExit 虚拟机退出的时候删除对应的文件
     * */
    public static void main(String[] args){
        File file1 = new File("D:\\input\\a\\b\\c\\d");
        File file2 = new File("D:\\output");
        File file = new File("D:\\input");
            try {
                System.out.println(file2.createNewFile());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(file1.mkdirs());
            System.out.println(file2.isFile());
            System.out.println(file2.isDirectory());
            System.out.println(file2.getName());
            System.out.println(file2.getAbsolutePath());
            System.out.println(file2.getPath());
            System.out.println(file2.getParent());
            System.out.println(file2.length());
            File [] list=file.listFiles();
            for(File f:list){
                System.out.println(f.getAbsolutePath());
            } 
    }
}

复制一个文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopZip {
    public static void main(String[ ]args){
        FileInputStream fi = null;
        FileOutputStream fo = null;
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;

        try {
            fi =new FileInputStream("D:\\input\\5EClient-2.1.9.zip");
            fo =new FileOutputStream("D:\\input\\5EClient-2.1.9(2).zip");
            bi = new BufferedInputStream(fi);

            bo = new BufferedOutputStream(fo);
            byte b [ ] = new byte[100];
            int len = 0;
            //这是带有缓冲区的拷贝 速度更快 效率更高
            try {
                while((len = bi.read(b))!=-1){
                    fo.write(b, 0, len);
                    bo.write(b);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bi!=null){
                try {
                    bi.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bo!=null){
                try {
                    bo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

拷贝一个文件

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyPic {
    /*
     * 基类:
     */
    public static void main(String[ ]args){
        FileInputStream fi = null;
        FileOutputStream fo = null;
        try {
            fi = new FileInputStream("D:\\input\\Copy.zip");
            fo = new FileOutputStream("D:\\input\\Copy(2).zip");
//          int len;
            //这是一个字节一个字节拷贝,速度非常慢
//          try {
//              while((len=fi.read())!=-1){
//                  fo.write(len);
//              }
//          } catch (IOException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
            byte b [ ] = new byte[1024];
            int len = 0 ;
            //这是1024个字节一次性拿出传入数组,一次性拷贝,速度比之前一个一个字节拷贝要快
            try {
                while((len = fi.read(b)) != -1){
                    fo.write(b);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fo!=null){
                try {
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值