io的5个类和三个接口以及File文件的一些操作方法

本文探讨了Java.io包中的关键组件,包括File类以及字节和字符流的输入/输出类:InputStream、OutputStream、Reader和Writer。同时介绍了重要的Closeable、Flushable和Serializable接口。此外,还概述了File类提供的文件操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于io的核心类,在Java.io包种最重要的就是5个类和3个接口,这5个类分别是File(文件类),InputStream(字节输入流),OutoutStream(字节输出流),Reader(字符输入流),Writer(字符输出流),3个接口分别是Closeable(关闭流接口),Flushable(刷新流接口),Serializable(序列化接口)

File文件的一些操作方法如下

package cn.com.io;

import java.io.File;

public class FileDemo01 {
    public static void main(String[] args) {
//        //File对象的三种构建方式
//        String path = "F:/java code/io_Study/abc.txt";
//        //1,构建File对象 直接丢路径
//        File file = new File(path);
//        System.out.println(file.length());
//        //2,构建File对象 父子构建
//        file = new File("F:/java code/io_Study","abc.txt");
//        System.out.println(file.length());
//        file = new File("F:/java code/","io_Study/abc.txt");
//        System.out.println(file.length());
//        //3,构建File对象 构建父对象,子名称
//        file = new File(new File("F:/java code/io_Study"),"abc.txt");
//        System.out.println(file.length());


//        String path = "F:java code/io_Study/abc.txt";
//        //绝对路径
//        File file = new File(path);
//        System.out.println(file.getAbsolutePath());
//        //相对路径
//        System.out.println(System.getProperty("user.dir"));
//        file = new File("abc.txt");
//        System.out.println(file.length());
//        System.out.println(file.getPath());
//        //构建一个不存在的文件
//        file = new File("abc/ball.png");
//        System.out.println(file.getAbsolutePath());

//        //文件的一些基本方法
//        File file = new File("F:/java code/io_Studey/abc.txt");
//        System.out.println("名称:"+file.getName());
//        System.out.println("路径:"+file.getPath());
//        System.out.println("绝对路径:"+file.getAbsolutePath());
//        System.out.println("获得父路径:"+file.getParent()); //没有返回null
//        System.out.println("父对象:"+file.getParentFile().getName()); //返回上一层的名称


//        //文件的状态 不存在:exists 存在 是文件还是目录
//        File file = new File("io_Studey/abc.txt");
//        System.out.println("是否存在:"+file.exists());
//        System.out.println("是否是文件:"+file.isFile());
//        System.out.println("是否是目录:"+file.isDirectory());
//
//        file = new File("abc.txt");
//        System.out.println("-------------");
//        System.out.println("是否存在:"+file.exists());
//        System.out.println("是否是文件:"+file.isFile());
//        System.out.println("是否是目录:"+file.isDirectory());
//
//        file = new File("F:/java code/io_Studey");
//        System.out.println("----------");
//        System.out.println("是否存在:"+file.exists());
//        System.out.println("是否是文件:"+file.isFile());
//        System.out.println("是否是目录:"+file.isDirectory());
//        //文件的一般操作流程如下
//        file =new File("***");
//        if(file == null || !file.exists()) {
//            System.out.println("文件不存在!");
//        }else if (file.isFile()) {
//            System.out.println("是文件");
//        }else {
//            System.out.println("是目录");
//        }

//        //length() 长度(返回的是文件的字节数)
//        File file = new File("F:/java code/io_Studey/abc.txt");
//        System.out.println("长度:"+file.length());
//        file = new File("F:/java code/io_Studey");
//        System.out.println("长度:"+file.length());

        //创建文件 createNewFile();
        File file = new File("F:/java code/io_Studey/io.txt");
        boolean flag = false;
        try {
            flag = file.createNewFile();
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(flag);
        flag = file.delete();
        System.out.println(flag);
        file = new File("F:/java code/io_Studey2");
        try {
            flag = file.createNewFile();
        }catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println(flag);
        flag = file.delete();
        System.out.println(flag);
    }
}

常用的五个函数(I/O) 1. 图像载入函数 函数cvLoadImage载入指定图像文件,并返回指向该文件的IplImage指针。函数支持bmp、jpg、 png、 tiff等格式的图像。其函数原型如下: IplImage* cvLoadImage( const char* filename, int iscolor); 其中,filename 是待载入图像的名称,包括图像的扩展名;iscolor是一个辅助参数项,可选正数、零负数三种值,正数表示作为三通道图像载入,零表示该图像作为单通道图像,负数表示载入图像的通道数由图像文件自身决定。 2. 窗口定义函数 函数cvNamedWindow定义一个窗口,用于显示图像。其函数原型如下: int cvNamedWindow( const char* name, unsigned long flags ); 其中,name是窗口名,flags是窗口属性指标值,可以选择CV_WINDOW_AUTOSIZE0两种值。CV_WINDOW_AUTOSIZE表示窗口尺寸与图像原始尺寸相同,0表示以固定的窗口尺寸显示图像。 函数 cvDestroyWindow(const char* name);销毁以上定义的窗口。 name是窗口名 3. 图像显示函数 函数cvShowImage是在指定的窗口中显示图像,其函数原型如下: void cvShowImage( const char* name, const CvArr* image ); 其中,name是窗口名称,image是图像型指针,一般是IplImage指针。 4. 图像保存函数 函数cvSaveImage以指定的文件名保存IplImage型的指针变量,其函数原型如下: int cvSaveImage( const char* filename, const CvArr* image ); 其中,filename是图像保存路径名称,image是IplImage指针变量。 5. 图像销毁函数 函数cvReleaseImage销毁已定义的IplImage指针变量,释放占用内存空间。其函数原型如下: void cvReleaseImage( IplImage** image ); 其中,image为已定义的IplImage指针
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值