【IO流】


I:Inout( 输入流:读取数据到内存)
O:Output(输出流:将数据输出到文件)
逐个数据按顺序处理,类似水流进行流动,称为IO流

Flie

File(文件的意思(文件和文件夹)): 对计算机来说文件就是指定盘符下的文件或者文件夹

File其中方法:
public File(String pathname) pathname:文件的路径;
创建文件:public boolean createNewFile() 创建文件,如果文件存在就不创建,返回false
创建文件夹:public boolean mkdir() 创建文件夹,如果文件夹存在,就创建,返回false,只能创建单个文件夹;
创建多层文件夹:public boolean mkdirs()
判断是否是文件夹:public boolean isDirectory()
判断是否是文件:public boolean isFile()
判断文件或目录是否存在 public boolean exists()
返回文件的长度,单位为字节 lenght

	
File file = new File("d:\\文件.txt");//在D盘创建一个txt类型的文件
boolean mkdir = file.mkdir();//创建一个文件夹
long length = file.length();//返回文件长度,单位为字节
System.out.println(file.exists());//判断文件/文件夹是否存在

字节流

字节输入流(FileInputStream):
字节输出流(FinleOutputStream):

字符流

字符流:  字符流的底层还是字节流, 字符流是专门处理文本的
字符输入流: FileReader
字符输出流: FileWriter

文件拷贝案例实现

import java.io.*;

public class Test {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        long start = System.currentTimeMillis();
        try {
            fis = new FileInputStream("D:\\lzb.txt");
            fos = new FileOutputStream("D:\\test\\lzb.txt");
            byte[] a = new byte[1024 * 512];
            int len = fis.read(a);
            while (len != -1) {
                fos.write(a, 0, len);
                len = fis.read(a);
            }
            long end = System.currentTimeMillis();
            System.out.println("拷贝完毕" + (end - start) + "ms");
        } catch (FileNotFoundException b) {
            throw new RuntimeException(b);
        } catch (IOException b) {
            throw new RuntimeException(b);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException b) {
                    throw new RuntimeException(b);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException b) {
                    throw new RuntimeException(b);
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值