栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

java学习--File对象与IO流

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java学习--File对象与IO流

1.File对象 1.1.File简介

File 类 就是当前系统中 文件或者文件夹的抽象表示 
通俗的讲  就是使用File对象来操作电脑系统中的文件或者文件夹

学习File类 其实就是学习 通过file对象 对系统中的文件/文件夹进行增删改查

1.2.创建File对象

        在磁盘准备一个练习用的文件夹,防止学习File中对别的文件误操作

        File file1 = new File("E:\Test\aaa.txt");

         
        //1. window 兼容 / 和 
        File file2 = new File("E:/Test/bbb.txt");

        //2. File.separator根据当前代码所在系统自动获取相应的分割符
        File file3 = new File("E:"+File.separator+"Test"+File.separator+"ccc.txt");
1.3.增加
        //增加操作
        File file1 = new File("E:/Test/aaa.txt");//创建File对象
        file1.createNewFile();//创建对应文件
        File file2 = new File("E:/Test/bbb");
        file2.mkdir();//创建目录 make directory 单层目录
        File file3 = new File("E:/Test/ccc/eee");
        file3.mkdirs();//创建多层目录
        File file4 = new File("E:/Test/fff");
        file4.createNewFile();//使根据调用的方法不同 而创建不同的类型
1.4.删除
        //删除操作
        File file1 = new File("E:/Test/fff");
        file1.delete();//删除文件

        File file2 = new File("E:/Test/aaa.txt");
        file2.deleteOnExit();//程序退出后删除

        Thread.sleep(10000);//休眠10秒

        File file3 = new File("E:/Test/bbb");
        file3.delete();//删除空目录
1.5.修改
        //修改操作

        File file = new File("E:/Test/aaa.txt");
        file.setReadable(false);//设置该文件得权限为不能读
        file.setWritable(false);//设置该文件不能写
        file.setReadOnly();//设置该文件只读

        file.renameTo(new File("E:/Test/x.txt"));//重命名
1.6.查询
        File file = new File("E:/Test/bbb/qqq.txt");
        String name = file.getName();//得到文件名
        System.out.println("name------"+name);

        String parent = file.getParent();//得到父级路径名
        System.out.println("parent------"+parent);

        String path = file.getPath();//得到文件的路径
        System.out.println("path-------"+path);


        boolean file1 = file.isFile();//判断是否为文件类型
        System.out.println("file1==========="+file1);
        boolean file2 = file.isDirectory();//判断是否为目录类型
        System.out.println("file2==========="+file2);

        File file3 = new File("E:/Test");
        String[] list = file3.list();//列出Test下所有子文件名称
        System.out.println(Arrays.toString(list));

        File[] files = file3.listFiles();//列出AAA目录下所有文件对象
        for(File f:files){
            System.out.println(f.toString());
        }

经典例题:查询指定目录下的所有文件(使用递归调用)

    public static void main(String[] args) {
        showAllFiles("E:/Test");//传入一个路径
    }
    public static void showAllFiles(String path){
        //调用传入的路径构建文件对象
        File file = new File(path);
        //判断文件是否存在 或者文件是否为目录
        if(!file.exists() || !file.isDirectory()){
            return;
        }
        //列出该目录下的所有文件对象
        File[] files = file.listFiles();
        //遍历数组
        for(File f:files){
            if(f.isDirectory()){
                System.out.println(f.getPath()+"   ");
                showAllFiles(f.getPath());//继续调用方法
            }else {
                System.out.println(f.getPath()+" ");
            }
        }
    }
2.IO流

1. IO 表示有两个单词的缩写。
  I: Input 输入   O: Output 输出

2. IO的作用:就是对文件中的内容进行操作。 
  输入: 读操作(读取文件的内容)   输出: 写操作(往文件中写内容)
  
3. IO流的分类:

 (1)根据流的方向:
    ---输入流: 程序可以从中读取数据的流。
    ---输出流: 程序能向其中写入数据的流。
    
 (2)根据流的单位:
    ---字节流: 以字节为单位传输数据的流
    ---字符流: 以字符为单位传输数据的流
 
 (3)根据功能
    ---节点流: 直接和文件进行交互
    ---处理流: 不是直接作用在文件上。
    
四个基本的流: 其他的流都是在这四个流的基础上进行扩展的
        字节输入流
        字节输出流 
    
        字符输入流
        字符输出流

2.1 Writer字符输出流

它是所有字符输出流的根类。---FileWriter类

public static void main(String[] args) throws Exception{
        //字符输出流 ---指定对哪个文件(路径)进行写操作
        Writer  writer=new FileWriter("D:/AAA/d.txt");

        String str="hello AAA 今天要开演唱会";
        writer.write(str);

        writer.flush(); //刷新流
        writer.close(); //关闭流资源
    }

 上面每次往文件中写内容时 就会把原来的内容覆盖了。 如何追加内容

public static void main(String[] args) throws Exception{
        //字符输出流 ---指定对哪个文件(路径)进行写操作
        //true:表示允许追加内容到文件中
        Writer  writer=new FileWriter("D:/AAA/d.txt",true);

        String str="Hello 刘德华 今天请来了";
        writer.write(str);

        writer.flush(); //刷新流
        writer.close(); //关闭流资源
    }
2.2 Reader字符输入流

它是所有字符输入流的根类  它的实现类有很多,我们使用FileReader实现类

public static void main(String[] args) throws Exception {
         //创建字符输入流对象. 作用:就是读取aaaa.txt里的内容
         Reader reader=new FileReader("D:/AAA/aaaa.txt");
         int count=0;//表示读取字符的个数
         char[] cs=new char[10]; //每次读取到元素存入该数组中
         while( (count=reader.read(cs)) !=-1 ){
                String str=new String(cs,0,count);
                System.out.print(str);
         }



        //但是这种效率会很慢 因为每次只读取一个字符。
        // 那么我们可以一次读取多个字符 并存入一个字符数组中
//        char[] cs =new char[2]; //每次读取2个字符放入该数组中。
//        int c=reader.read(cs);// 每次读取两个字符放入cs数组中 返回读取的个数
        System.out.println(c);
        System.out.println(cs);
//        String s=new String(cs,0,c);//把字符数组转换为字符串。
//        System.out.println(s);
//
//        c = reader.read(cs);
        System.out.println(c); //
        System.out.println(cs); // [a,b]-->[c,b]
//        String s1=new String(cs,0,c);
//        System.out.println(s1);
//
//        c = reader.read(cs);
//        System.out.println(c); //
        System.out.println(cs); // [a,b]-->[c,b]



    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号