IO

 

File

->  内存对象,表示文件/目录,和本地有没有这个文件/目录没关系

 内存中创建一个file对象,和本地存不存在文件无关

 

java.io:input输入/读  output输出/写

文件单位:字节

人类读:字符编码  UTF-8  GBK

 

字节流:InputStream         OutputStream

字符流:Reader                      Writer

 

路径:

绝对路径:C:\User\Desktop\a.txt

Linux:根目录/

/home/Bonnie/Desktop/a.txt

 

相对路径:参照物

当前工作目录,现在工作所在的目录(正在打开的目录)

..上一级

.当前

 

路径代码

// 获得的是绝对路径,通过相对路径获得绝对路径

     // 相对FileDemo.class文件所在目录 相当于类所在包

/* 基于包*/

     String pathB1 = FileDemo.class.getResource("b.txt").getFile(); // getClass()

/* 基于src*/

String pathB = FileDemo.class.getClassLoader().getResource("day0518/b.txt").getFile();// getClass()

 

File方法

        File file = new File("a.txt");

        File file1 = new File("d.txt");

        File file2 = new File("e.txt");

        //所表示的文件/文件夹 存不存在

        boolean isExists = file.exists();

        // 1.问file对象是不是文件(存在)

        file.isFile();

        // 2.问file对象是不是文件夹(存在)

        file.isDirectory();

        // 3.获得file对象的文件大小(存在)

        file.length();

        // 4.获得file对象最后修改时间(存在)

        file.lastModified();

        // 5.创建文件(不存在)

        file1.createNewFile();

        // 6.创建文件夹(不存在)

        file2.mkdir();// 创建指定目录

        file2.mkdirs();// 不存在的父目录一起创建

        // 7.获得file对象的名字

        file.getName();

        // 删除文件(只能删文件或空目录)

        file.delete();

 

 

// 列出当前file对象中所有的file(文件/目录)

        File file = new File("src/day0518");

        File[] fs = file.listFiles();

        for (File f : fs){

            System.out.println(f);

        }

// 列出当前file对象中所有的file(文件/目录) 过滤器

        File file = new File("src/day0518");

        File[] fs = file.listFiles(new FileFilter() {

            /**

             * 过滤f对象

             * 返回true,f对象保留

             * 返回false,f被过滤

             */

            public boolean accept(File f) {

                // 保留所有的java文件  .java

                return f.getName().endsWith(".java");

            }

        });

        for (File f : fs){

            System.out.println(f);

        }

------------------------------------------------------------------------------------------------------------------------------------

字节流:InputStream / OutputStream

如果没有文件,创建新文件

如果有文件,清空原有文件内容,再写入 - false(默认)

如果有文件,在原有内容后追加新的内容 - true

    InputStream fis = new FileInputStream("a.txt");

    OutputStream fos = new FileOutputStream("a.txt", true);

FileInputStrean(File)

    FileInputStream fis = new FileInputStream("jh.png");

FileOutputStream(File, boolean)

    FileOutputStream fos = new FileOutputStream("jh111.png", true);

BufferedInputStream

    InputStream is = new FileInputStream("a.txt");

    BufferedInputStream bis = new BufferedInputStream(is);

BufferedOutputStream

    OutputStream os = new FileOutputStream("a.txt");

    BufferedOutputStream bos = new BufferedOutputStream(os);

字节为单位,不是用来读写可视内容的,通常用来复制文件,又因为缓冲流效率高,所以复制文件用缓冲流

底层流就是用来生成其他高级流的

 

 

字符流:Reader / Writer

InputStreamReader

    InputStream is = new FileInputStream("a.txt");

    Reader isw = new InputStreamReader(is, "gbk");

OutputStreamReader


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值