高级特性三|IO流

IO流

File

.exists()判断该文件是否存在
.delete()删除该文件
.isFile()判断是否是文件
.getName()获取文件名
.getPath()获取相对路径
.getAbsolutePath()获取绝对路径
.length()获取文件大小 (字节长度)
.isDirectory()判断是否是目录
.createNewFile()创建该文件 注: 需try catch包含
   try {
               file.createNewFile();
               System.out.println("文件已创建");
           } catch (IOException e) {
               e.printStackTrace();
           }

字节输入流

流 是指一连串流动的字符,是以先进、先出方式发送信息的通道 字节流是8位通用字节流,字符流是16位Unicode字符流

OutputStream和Writer作为基类

声明方式FileInputStream file = new FileInputStream(“读取输入路径”);
.available()读取字节数
.read()从输入流中读取数据的下一个字节 如果已经没有可用的字节,则返回-1
.close()关闭读取操作 在finally中声明

字节输出流

InputStream和Reader作为基类

声明方式FileOutputStream file = new FileOutputStream(输入路径)
.getBytes()返回值为byte[]数组 得到一个ASCII编码格式的字节数组
假设一个字符串 str=“abc” byte[] bytes = str.getBytes[] 则bytes[0]=97 , bytes[1]=98 , bytes[2]=99
.write(byte[] , 起始位置(0) , 结束位置(byte[].length))写入数据
write每次读取10个字节 若字节位46 则多出4字节 以获取的长度为结束位置则可避免该问题
自写程序 复制文件内容
	    FileInputStream file = null;
        FileOutputStream file1 = null;
        File file2 = null;
        try {
            //创建输出流对象 用于读取文件对象创建
             file = new FileInputStream("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\字节输出流\\text.txt");
            //创建输入流对象 用于对目标文件写入数据
            file1 = new FileOutputStream("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\复制文本文件\\text.txt");
            //判断是否有该文件
            file2 = new File("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\复制文本文件\\text.txt");
            byte[] words = new byte[file.available()];
            //如果没有目标文件 则自动创建
            if (!file2.exists()){
                System.out.println("您的目录下没该文件 已自动为您创建");
                file2.createNewFile();
            }
            //写入文件操作
            while ((file.read(words))!=-1){
                file1.write(words , 0 , words.length);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                file.close();
                file1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

字符流

字符输入流

声明方式FileReader fr = new FileReader(读取路径);
.readLine()读取数据 以字符串形式返回这一行的数据
当读取完所有的数据时会返回null。
.replace(原字符,新字符)第一个字符为查找的字符 第二个字符为查找后 需要替换成的字符
输出文档内容:
		   ileReader fr = null;
        try {
            //创建FileReader对象
            fr = new FileReader("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\字符输入流\\text.txt");
            //创建字符数组作为中转站
            char[] ch = new char[1024];
            //用于临时储存读取的数据
            StringBuffer sbf = new StringBuffer();
            int length = -1;
            //开启循环
            while ((length = fr.read(ch))!=-1){
                sbf.append(ch); //追加
            }
           //输出
            System.out.println(sbf.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

每次读取一行 效率较高

FileReader fr = new FileReader();
BufferedReader bf = new BufferedReader(fr);
	    FileReader fr = null;
        BufferedReader bf = null;
        try {
            //创建对象
            fr = new FileReader("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\复制文本文件\\text.txt");
            bf = new BufferedReader(fr);
            String line = null;
            //开启循环
            while (( line = bf.readLine())!=null){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr!=null){
                    fr.close();
                }
                if (bf!=null){
                    bf.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

字符输出流

FileWriter fw = new FileWriter(目标文件路径);
.flush()刷新缓冲区
.write()写入字段
.toString()转换为字符串输出
try {
            //创建一个FileWriter文件
            fw = new FileWriter("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\字符输出流\\text.txt");
            fw.write("姚小煜");
            fw.flush();//刷新缓冲区
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

写入效率高的写法

FileReader fr = new FileReader();
BufferedReader bf = new BufferedReader(fr);
.newLine()可起到换行作用
	    BufferedWriter bf = null;
        FileWriter fw = null;
        try {
           fw= new FileWriter("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\字符输出流\\text.txt");
            bf = new BufferedWriter(fw);
            bf.write("你好");
            bf.newLine(); //换行
            bf.write("好久不见");
            bf.newLine();
            bf.write("最近还好么");
            bf.flush(); //刷新缓冲区
            System.out.println(bf.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

写入二进制文件

可复制图片 代码如下

 		FileInputStream fis = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        DataOutputStream dos = null;
        try {
            //创建输入流对象
            fis = new FileInputStream("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\写二进制文件\\text.jpg");
            dis = new DataInputStream(fis);
            //创建输出流对象
            fos = new FileOutputStream("E:\\javaWorkSpace\\高级特效\\第三章IO\\src\\写二进制文件\\刘振阳.jpg");
            dos = new DataOutputStream(fos);

            //开始读写二进制
            int temp;
            while ((temp = dis.read())!=-1){
                dos.write(temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis!=null){
                    fis.close();
                }
                if (dis!=null){
                    dis.close();
                }
                if (fos!=null){
                    fos.close();
                }
                if (dos!=null){
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值