Java高级--I/O流

I/O流

概念:

与硬盘交互,储存信息

流是一种抽象概念,他代表了数据的无结构化传递,按照流的方式进行输入和输出,数据被当成无结构的字节序或字符序列。

I/O流就是以流的方式进行输入输出。

 以系统为主体,硬盘>>>系统(输入)   系统>>>硬盘(输出)

File类

概述:

是文件和目录路径名的抽象表现形式。

常用方法:

creatNewFile            创建名称的空文件,不创建文件夹             boolean

isDirectory()               判断是否是目录                                        boolean

isFile()                       判断是否是文件                                         boolean

exists()                      判断文件或者目录是否存在                       boolean

getAbsolutePath()返回此对象表示的文件的绝对路径名         string

getPath()                   返回此对象表示的文件的相对路径名         string

getName()                 返回此对象表示的文件或目录的名称         string

length()                     返回文件长度,单位为字节                        long

mkdir()                      创建目录

mkdirs()                    创建多级目录

 方法应用示例

public class FileTest {
    public static void main(String[] args) {
        File file = new File("E:\\abc.txt");
        try {
            System.out.println("创建新文件"+file.createNewFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("判断是否是目录"+file.isDirectory());
        System.out.println("判断是否是文件"+file.isFile());
        System.out.println("判断文件是否存在"+file.exists());
        System.out.println("绝对路径"+file.getAbsolutePath());
        System.out.println("相对路径"+file.getPath());
        System.out.println("获取名称"+file.getName());
        System.out.println("获取长度"+file.length());
        System.out.println("创建目录"+file.mkdir());
        System.out.println("创建多级目录"+file.mkdirs());

    }
}

 字节输入流和字节输出流

字节输入流(InPutStream)    字节输出流(OutPutStream)

通过流的方式获取文件长度:.available();

刷新缓冲区:.flush();

关闭流通道:.close();

获取系统当前时间:currentTimMillis();

应用(数据传输):

public static void main(String[] args) {
        //字节输入流
        InputStream inputStream = null;
        //带缓冲区的字节输入流
        BufferedInputStream bufferedInputStream = null;
        //字节输出流
        OutputStream outputStream = null;
        //带缓冲区的字节输出流
        BufferedOutputStream bufferedOutputStream = null;
        try {
            inputStream = new FileInputStream("E:\\io\\Java.rar");
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = new FileOutputStream("E:\\oi\\Java.rar");
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            //获取系统当前时间
            long l = System.currentTimeMillis();
            //通过流获取文件长度
            //分批次复制文件
            //最佳数组长度需要多次进行测试得到
            byte[] bytes = new byte[1024 * 50];
            //读取
            int read = bufferedInputStream.read(bytes);
            //判断是否有读回来的内容  -1表示没有了
            while (read != -1){
                //每次写入时只写入读回来的长度
                bufferedOutputStream.write(bytes,0,read);
                //写完一批在读一批,在此进入循环写入
                read = bufferedInputStream.read(bytes);
            }

            long l1 = System.currentTimeMillis();
            System.out.println("复制文件所需时间" + (l1-l));
            //清理缓冲区,刷新
            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                //先判断流通道是否开启,开启了才能关
                if (bufferedOutputStream != null)
                    bufferedOutputStream.close();
                //后来者先关
                if (outputStream != null)
                    outputStream.close();
                if (bufferedInputStream != null)
                    bufferedInputStream.close();
                //开了通道之后一定得关
                //关流
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

字符输入流与字符输出流

字符输入流(read)      字符输出流(write)

应用和字节输入和输出一致

 public static void main(String[] args) {
        //字符输入流
        Reader reader = null;
        BufferedReader bufferedReader = null;
        //字符输出流
        Writer writer = null;
        BufferedWriter bufferedWriter = null;
        try {
            reader = new FileReader("E:\\abc.txt");
            bufferedReader = new BufferedReader(reader);
            writer = new FileWriter("E:\\cba.txt",true);
            bufferedWriter = new BufferedWriter(writer);
            char[] chars =new char[16];
            bufferedReader.read(chars);
            //foreach循环遍历
            for (char c:chars) {
                System.out.println(c);
            }
            bufferedWriter.write(chars);
            bufferedWriter.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (bufferedWriter != null)
                    bufferedWriter.close();
                if (writer != null)
                    writer.close();
                if (bufferedReader != null)
                    bufferedReader.close();
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值