栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

File对象 IO流

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

File对象 IO流

1.File对象 1.1 File的介绍

File类就是当前系统中 文件或者文件夹的抽象表示

通俗地讲就是使用File对象来操作我们电脑系统中的文件或者文件夹

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

1.2创建File对象
 File file = new File("D:/AAA/aaaa.txt");
1.3添加操作

createNewFile() : 创建文件      mkdir(); : 创建单级目录       mkdir():创建多级目录

 public static void main(String[] args) throws IOException {
        File file = new File("D:/AAA/aaaa.txt");
        file.createNewFile();//创建文件
        File file1 = new File("D:/AAA/aaa");//
        file1.mkdir();//创建单级目录
        File file2 = new File("D:/AAA/aaaa");
        file2.createNewFile();
        File file3 =new File("D:/AAA/ssss/ffff/jjjj");
        file3.mkdirs();//创建多级目录
    }
1.4.删除操作

delete();  : 删除文件或删除空文档      deleteOnExit();  : 当程序执行完在删除

     public static void main(String[] args) throws InterruptedException {
        File file = new File("D:/AAA/aaaa.txt");
        file.delete();//删除文件
        File file1 = new File("D:/AAA/aaaaa.txt");
        file1.deleteOnExit();//当程序执行完在删除
        //Thread.sleep(5000);
        File file2 = new File("D:/AAA/bbb");
        file2.delete();//只能删除空文档
    }
1.5.修改操作

setReadable(false); :设置文件权限不能读

setWritable(false); :设置文件权限不能写

setReadOnly();:设置只读权限

    public static void main(String[] args) throws IOException {
        File file = new File("D:/AAA/aaa/aaa.txt");
        //file.mkdir();
        file.createNewFile();
        file.setReadable(false);//设置文件权限不能读
        file.setWritable(false);//设置文件不能写
        file.setReadOnly();//设置只读权限
        file.renameTo(new File("D:/AAA/aaa/d.txt"));//重命名
    }
1.6.查询操作

file.getName();:得到文件的名字

file.getParent();:得到父级路径的名称

file.getPath();:得到文件的路径名称

file.isFile();:判断文件是否为文件类型

file.isDirectory();:判断文件是否为目录类

file.list();:列出AAA文件下的所有子文件的名称

file1.listFile();:列出AAA目录下所有文件对象

     public static void main(String[] args) {
        File file = new File("D:/AAA/aaa/d.txt");
        String name = file.getName();//得到文件的名字
        System.out.println(name);
        String parent = file.getParent();//得到父及路径的名称
        System.out.println(parent);
        String path = file.getPath();//得到文件的路径名称
        System.out.println(path);
        boolean F1 = file.isFile();//判断文件对象是否为文件类型
        System.out.println("f1====>"+F1);
        boolean f2 = file.isDirectory();//判断文件对象是否为目录类
        System.out.println("f2====>"+f2);

        File file1 = new File("D:/AAA");
        String[] list = file1.list();//列出AAA目录下的所有子文件的名称
        System.out.println(Arrays.toString(list));

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

    }
2.IO流 2.1 IO流的介绍

1. IO表示两个单词的缩写:

        I:Input  输入        O:Output输出

2. IO的作用:就是对文件中的内容进行操作。

输入:读文件(读文件的内容)  输出:写操作(往文件中写内容)

3. IO流的分类:

(1)根据流方向分类:

                输入流:程序从文件中读取到程序

                输出流:程序向文件中写入数据

(2)根据流的单位分类:

                字节流:以字节为单位传输数据

                字符流:以字符为单位传输数据

(3)根据功能分类:

                节点流:直接与文件进行交互

                处理流:不直接作用在文件上

4. 四大基本流:其他的流都是在此基础上拓展的

        字节输入流(Reader)                      字节输出流(Writer)

        字符输入流(InPutStream)              字符输出流(OutputStream

5.具体方法:

write(); : 添加                flush();:刷新管道                close();:关闭文件

read();:个数                

2.2字符流 2.2.1 Writer字符输出流

    1.每次运行后,之前的内容会被覆盖

     public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("D:/AAA/q.txt");
        fw.write("今天天气很好");
        fw.flush();
        fw.close();
    }

2.每次运行后不会被覆盖

    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("D:/AAA/q.txt" ,true);
        fw.write("今天天气好热");
        fw.flush();
        fw.close();
    }
2.2.2Reader字符输入流
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:/AAA/q.txt");
        int a = 0;//用于记录char里面的元素个数
        char[] c = new char[10];//定义长度为10的char数组
        while ((a = fr.read(c)) != -1){//fr.read(c);表示在fr中每次取读10个元素
            String str = new String(c,0,a);//在c中,从0开始取读长度为a的数组
            System.out.print(str);
        }
        fr.flush();
        fr.close();
    }
2.2.3 复制字符文件

Reader与Writer的运用,只适用于TXT文件。

    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:/AAA/q.txt");//读取文件对象
        FileWriter fw = new FileWriter("E:/AAA/a.txt",true);//要写文件对象
        int s = 0;
        char[] c = new char[10];
        while ((s = fr.read(c)) != -1){
            fw.write(c,0,s);
        }
        fr.close();
        fw.close();
    }
2.3字节流 2.3.1 OutputStream字节输出流

    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("D:/AAA/dvdd.txt");
        String str = "经理付款即可";
        byte[] bytes = str.getBytes();//str字符串转为byte字节型
        os.write(bytes);
        os.flush();
        os.close();
    }
2.3.2 InputStream字节输入流
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("D:/AAA/dvdd.txt");
        int a = 0;//用于存储指针移动的次数
        byte[] c = new byte[10];
        while ((a = is.read(c)) != -1){//当is.read(c)等于-1时结束
            String str = new String(c,0,a);
            System.out.print(str);
        }
        is.close();
    }
2.3.3 复制文件

InputStream字节输入流与OutoutStream字节输出流的运用

    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("E:/AAA/2.mp4");
        InputStream is = new FileInputStream("D:/AAA/1.mp4");
        int a =0 ;
        byte[] bytes = new byte[10];
        while ((a = is.read(bytes)) != -1){//将is内容转化为10byte的长度格式
            os.write(bytes);//将bytes添加到os
        }
        os.close();
        is.close();
    }
2.4缓存流

缓存流是在基础流[InputStream  OutputStream  Reader    Writer]之上 添加了一个缓存池功能

BufferInOutStream BufferOutputStream ButterReader BufferWriter 提高IO的效率,降低IO的次数 

    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("E:/AAA/AA.txt");
        BufferedOutputStream bos = new BufferedOutputStream(os);
        String str = "九分裤见附件啊";
        byte[] bytes = str.getBytes();
        bos.write(bytes);//截止到此处;因为你写的内容暂时被放入了缓冲池,并没有直接放入文件中, 
                         //所以文件中没有内容
        bos.flush();
        bos.close();
    }
2.5对象流--对java对象进行IO操作

1.序列化:把内存中的java对象存储到磁盘(网盘)的过程。

                ----java对象所属的类必须实现序列化接口,implements Serializable

2.反序列化:把内存中的内容读取到java对象内存中的过程。

存档--------序列化

   public static void main(String[] args) throws IOException {
        OutputStream out = new FileOutputStream("E:/AAA/fjaj.txt");
        ObjectOutputStream oss = new ObjectOutputStream(out);
        Role r = new Role("周瑜",5236412);
        oss.writeObject(r);
        oss.close();
    }
public class Role implements Serializable {
    private String name;
    private int id;

    public Role(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + ''' +
                ", id=" + id +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

读档------反序列化

    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("E:/AAA/fjaj.txt");
        ObjectInputStream ois = new ObjectInputStream(is);
        Object o = ois.readObject();
        System.out.println(o);
        ois.close();
    }
2.6总结

1.通过字符流完成文件的复制------->他只能复制文本文件

2.字节流:字节输入流和字节输出流。

3.字节流:我们也可以完成文件的复制功能====他可以复制任意类型的文件

4.缓存流:他在基本流的基础上 添加了一个缓存池

5.对象流:ObjectInputStream        ObjextputStream

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

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

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