File 类,InputStream, OutputStream 的用法

目录

一.File类

关于名字和路径的操作

关于创建和销毁的操作

创建文件夹(多级目录)

 InputStream

第一种:字节流读取

第二种: 字符流读取(Reader)

 OutputStream 

第一种:字节流写入

 第二种方式:字符流输入


一.File类

File翻译过来"文件"

那么File类的操作实际上就全部是对文件进行操作

关于名字和路径的操作

    public static void main(String[] args) {
//虚拟一个名字叫做test.txt的文件,但这一步只是虚拟的,还没有创建
        File file = new File("./test.txt");
        System.out.println(file.getParent());//获取这个文件的父目录的路径
        System.out.println(file.getName());//获取这个文件的名字
        System.out.println(file.getPath());//获取这个文件的相对路径
        System.out.println(file.getAbsolutePath());//获取这个文件的绝对路径
    }

关于创建和销毁的操作

    public static void main(String[] args) throws IOException {
//拟创建一个叫做Test.txt的文件,但还没创建
        File file = new File("./Test.txt");
//真的在计算机的当前目录创建了这个文件
        file.createNewFile();
        System.out.println(file.isFile());//判断是否是普通文件
        System.out.println(file.isDirectory());//判断是否是目录文件
        System.out.println(file.exists());//判断这个文件是否存在
        System.out.println(file.delete());//删除这个文件
        System.out.println(file.exists());
    }

创建文件夹(多级目录)

    public static void main(String[] args) {
        File file = new File("./hello1");
        File file2 = new File("./hello2/6666");
        file.mkdir();//只能创建一级目录
        file2.mkdirs();//能创建多级目录
    }

 InputStream

就是从文件里面读取数据

一般有两种读取方式

第一种:字节流读取

(读写的基本单位是字节)

基本方法: read() :读取一个字节的数据,返回-1代表读取完毕

使用

我们先在D盘创建一个名字叫做hello.txt的文件,然后输入hello

    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = new FileInputStream("D:/hello.txt");//这里输入读取的文件地址,如果输入错误会报错无法读取
        while(true){
            try {
                int ret = inputStream.read();//如果全部读取完毕返回-1
                if(ret == -1){
                    break;
                }
                System.out.println(ret);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

输出的为对应的ASCII码~

第二种: 字符流读取(Reader)

运行原理和字节流读取一样,唯一不同的是在屏幕上显示的是字符而不是ask表对应的数字

    public static void main(String[] args) throws FileNotFoundException {
        Reader reader = new FileReader("D:/hello.txt");
        while(true){
            try {
                int ret = reader.read();
                if(ret == -1){
                    break;
                }
                System.out.println((char)ret);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

 OutputStream 

就是向文件里面写数据

一般有两种写入方式

第一种:字节流写入

基本方法: write() 

    public static void main(String[] args) throws FileNotFoundException {
        OutputStream outputStream = new FileOutputStream("d:/hello.txt");
        try {
//注意,这里的98,99,100是ask码表对应的数字的字符,不是数字98,99,100
            outputStream.write(98);
            outputStream.write(99);
            outputStream.write(100);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

 第二种方式:字符流输入

这种方式一定要注意在最后使用flush方式

把存储在内存中的字节流拿出来

否则会什么都没有但是程序依然不报错

    public static void main(String[] args) throws IOException {
        Writer writer = new FileWriter("d:/hello2.txt");
        writer.write("hello");
//一定要加上flush!!!!!
//一定要加上flush!!!!!
//一定要加上flush!!!!!
        writer.flush();
    }

猜你喜欢

转载自blog.csdn.net/qq_62718027/article/details/131351999